two –dimensional arrays mrs. c. furman september 18, 2008

11
Two –Dimensional Two –Dimensional Arrays Arrays Mrs. C. Furman Mrs. C. Furman September 18, 2008 September 18, 2008

Upload: annabelle-miller

Post on 04-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Two –Dimensional Arrays Mrs. C. Furman September 18, 2008

Two –Dimensional ArraysTwo –Dimensional ArraysMrs. C. FurmanMrs. C. Furman

September 18, 2008September 18, 2008

Page 2: Two –Dimensional Arrays Mrs. C. Furman September 18, 2008

The “for each” loopThe “for each” loop A shortcut with for loops.A shortcut with for loops. Used to iterate through a sequence of elements Used to iterate through a sequence of elements

in an array or ArrayList.in an array or ArrayList. The body of the loop is executed for each The body of the loop is executed for each

element in the array or ArrayList.element in the array or ArrayList. At the beginning of each iteration, the next At the beginning of each iteration, the next

element is assigned to the control variable.element is assigned to the control variable. Works the same as a standard for loop that Works the same as a standard for loop that

loops through all elements of the data structure. loops through all elements of the data structure. The control variable in “for each” is assigned to The control variable in “for each” is assigned to

the value at each index, where the standard for the value at each index, where the standard for loop is being assigned the index.loop is being assigned the index.

Page 3: Two –Dimensional Arrays Mrs. C. Furman September 18, 2008

The “for each” loop structureThe “for each” loop structure

for (for (Type variable Type variable :: collection collection))statementsstatements

Example:Example:int [] myList = {1, 3, 5, 7, 9};int [] myList = {1, 3, 5, 7, 9};int sum = 0;int sum = 0;for (int i : myList)for (int i : myList) sum += i;sum += i;

Page 4: Two –Dimensional Arrays Mrs. C. Furman September 18, 2008

Example 1Example 1

Write a “for each” loop that prints all Write a “for each” loop that prints all elements in the array elements in the array data.data.

Page 5: Two –Dimensional Arrays Mrs. C. Furman September 18, 2008

Example 2Example 2

Re-write the following loop using for each: Re-write the following loop using for each: list is an ArrayList of Double.list is an ArrayList of Double.

List <Double> mylist = new ArrayList <Double> ();List <Double> mylist = new ArrayList <Double> ();

for (int i = 0; i < mylist.size(); i++)for (int i = 0; i < mylist.size(); i++)

{{

System.out.println (mylist.get(i));System.out.println (mylist.get(i));

}}

Page 6: Two –Dimensional Arrays Mrs. C. Furman September 18, 2008

Two Dimensional ArraysTwo Dimensional Arrays

rows and columns, also called a Matrixrows and columns, also called a Matrix we must specify the number of row and columns we must specify the number of row and columns

we will need when we construct the 2DArray. we will need when we construct the 2DArray. Similar to Arrays.Similar to Arrays.

Example:Example:

int [][] magicSquare = new int [4][4];int [][] magicSquare = new int [4][4]; 00 00 00 00

00 00 00 00

00 00 00 00

00 00 00 00

0 1 2 30

1

2

3

Page 7: Two –Dimensional Arrays Mrs. C. Furman September 18, 2008

Accessing Elements in a 2D ArrayAccessing Elements in a 2D Array

Elements are indexed [row][column]Elements are indexed [row][column]

magicSquare [0][0] = 3;magicSquare [0][0] = 3;

magicSquare [1][3] = 5;magicSquare [1][3] = 5;

int myNum = magicSquare [1][3];int myNum = magicSquare [1][3]; 33 00 00 00

00 00 00 55

00 00 00 00

00 00 00 00

0 1 2 30

1

2

3

Page 8: Two –Dimensional Arrays Mrs. C. Furman September 18, 2008

Initializer List for 2DInitializer List for 2Dint [][]cars = {{10, 7, 12, 10, 4},int [][]cars = {{10, 7, 12, 10, 4},

{18, 11, 15, 17, 10},{18, 11, 15, 17, 10},

{12, 10, 9, 5, 12},{12, 10, 9, 5, 12},

{16, 6, 13, 8, 3}};{16, 6, 13, 8, 3}};

1010 77 1212 1010 44

1818 1111 1515 1717 1010

1212 1010 99 55 1212

1616 66 1313 88 33

GM

Ford

Toyota

BMW

red brown black white gray

Page 9: Two –Dimensional Arrays Mrs. C. Furman September 18, 2008

Getting SizeGetting Size

.length gives us the number of rows… .length gives us the number of rows… then when we access a specific row, we then when we access a specific row, we do .length to get number of columns.do .length to get number of columns.

int[][] list = {{1,2,3, 4}, {1}, {1, 2, 3}};int[][] list = {{1,2,3, 4}, {1}, {1, 2, 3}}; int numRows = list.length;//3int numRows = list.length;//3 int firstCol = list[0].length;//4int firstCol = list[0].length;//4 int secCol = list[1].length;//1int secCol = list[1].length;//1 int thirdCol = list[2].length;//3int thirdCol = list[2].length;//3

Page 10: Two –Dimensional Arrays Mrs. C. Furman September 18, 2008

Looping Through a 2D ArrayLooping Through a 2D Array

Example 3: Write a nested for loop to output Example 3: Write a nested for loop to output all elements in the 2D array.all elements in the 2D array.

Page 11: Two –Dimensional Arrays Mrs. C. Furman September 18, 2008

Multiplication TableMultiplication Table Write a loop that will assign a 5 x 5 2D Array to Write a loop that will assign a 5 x 5 2D Array to

the multiplication tables of 0 ..4.the multiplication tables of 0 ..4.

The result will look like the below 2D ArrayThe result will look like the below 2D Array

00 00 00 00 00

00 11 22 33 44

00 22 44 66 88

00 33 66 99 1212

00 44 88 1212 1616

0 1 2 3 4

0

1

2

3

4