CSE 14 Lecture Notes - Lecture 15: 5,6,7,8

49 views6 pages
22 May 2018
School
Course
Professor
CMPS12A Lecture 15 Multi-dimensional Arrays with Objects and Classes
We have dealt with one-dimensional arrays all this time where it is just simple to store items in
a single array. One would be able to access such memory locations by doing something like:
Example:
“trig [] arra = {hello, goode, orld};
System.out.println(array[1]);
This ould prit out the first ide of the arra arra of “trig tpe hih is goode.
Remember that indices start at the 0th index, so array[0] would print out hello.
Now, what if we had arrays within an array, which in turn is a 2-Dimensional Array.
Example:
doule [] [] tale = { {., ., ., ., .}, {., ., ., ., .}, {…, …, …, …, …}}
What this means is that table is initiated to a 2-dimensional array of double type. We have
arrays within an array and we need to understand that table has 3 arrays inside of the greater
array, right? We consider these arrays inside of the bigger array (should say outer array) to be
items of it. Thus, we see here that there are 3 items in the outer array, which is table.length
right? No, sie e hae  ites here, let’s look at the first ite hih is at the th index and
see an array of 5 items of doubles -> 1.1, 2.2, 3.3, 4.4, 5.5
To access 3.3, we would say:
table[0][2], where 0 is the first item (0th index) of the outer array and 2 is the 3rd item(2nd index)
of the array that is in the 0th index of the outer array. To get our length of this array within, we
would say:
table[0].length where [0] is telling us it is a 2-dimensional array and we want the length
of the array at the 0th index of the outer array.
Let’s look at our nd item in the table array (1st index) which consists of an array with items
->2.0, 3.0, 4.0, 5.0, 6.0
To access 6.0, we would say:
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in
table[1][4] where 1 tells us it is the 2nd item(index 1) of the outer array and 4 is the
5th item(index 4) of the inner array at index 1 of outer array.
To get our length of this array, we would use:
table[1].length
Notice the pattern? We use two brackets [] [] where the first [] is the outer array that looks how
many items (arrays) are inside and the second [] tells us how many items are inside the inner
arrays, hence, 2-dimensional arrays.
Another example
Example:
class TwoDimensionalArray1 {
public static void main(String [] args) {
int [] [] twoD = {{1, 2}, {3, 4, 5, 6}, {7, 8, 9}}
}
}
So, what this looks:
twoD (index)
0 1
0 1 2
0 1 2 3
1 3 4 5 6
0 1 2
2 7 8 9
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Cmps12a lecture 15 multi-dimensional arrays with objects and classes. We have dealt with one-dimensional arrays all this time where it is just simple to store items in a single array. One would be able to access such memory locations by doing something like: This (cid:449)ould pri(cid:374)t out the first i(cid:374)de(cid:454) of the arra(cid:455) (cid:862)arra(cid:455)(cid:863) of tri(cid:374)g t(cid:455)pe (cid:449)hi(cid:272)h is good(cid:271)(cid:455)e. Remember that indices start at the 0th index, so array[0] would print out hello. Now, what if we had arrays within an array, which in turn is a 2-dimensional array. Example: dou(cid:271)le [] [] ta(cid:271)le = { {(cid:1005). (cid:1005), (cid:1006). (cid:1006), (cid:1007). (cid:1007), (cid:1008). (cid:1008), (cid:1009). (cid:1009)}, {(cid:1006). (cid:1004), (cid:1007). (cid:1004), (cid:1008). (cid:1004), (cid:1009). (cid:1004), (cid:1010). (cid:1004)}, { , , , , }} What this means is that table is initiated to a 2-dimensional array of double type. We consider these arrays inside of the bigger array (should say outer array) to be items of it.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents

Related Questions