Chapter 07 Arrays not Array Lists for now

  • Slides: 24
Download presentation
Chapter 07 Arrays (not Array. Lists for now)

Chapter 07 Arrays (not Array. Lists for now)

Reasoning on using arrays

Reasoning on using arrays

int week. Days = 7; 7 week. Da ys Would you use this method

int week. Days = 7; 7 week. Da ys Would you use this method to create variables for the 40000 Pitt students?

Coworker Presents Student Presents Give me the 2 nd present from the Coworker array

Coworker Presents Student Presents Give me the 2 nd present from the Coworker array Give me the 3 rd present from the Student array

Creating 1 D arrays

Creating 1 D arrays

1 D Arrays

1 D Arrays

If I could open that “box” labeled “a”, what would I see inside it?

If I could open that “box” labeled “a”, what would I see inside it? Will it have all the 12 integer values in it? (thing about the Elephant example) The array “a” contains a reference of where the array is placed in memory (0 x 12) a

int dozen = 12; double [ ] sensor. Data = {4. 1, 15. 3,

int dozen = 12; double [ ] sensor. Data = {4. 1, 15. 3, 7. 7, 9. 1}; double pi = 3. 14; String message = “This is a long string”; int days. Of. The. Week =7; char letter = ‘A’; A B C D E F G H I 1 12 0 x 3 B 3. 14 0 x 3 F 7 A 13 2 T 2 11. 2 53. 1 2 4 9 a W z 6 3 14 4. 1 15. 3 7. 7 9. 1 T h i s 4 ““ i s ““ a ““ l o n 5 g ”“ s t r i n g 6 7 8

2 D Arrays

2 D Arrays

2 D Arrays (tables) double [ ][ ] a = new double[3][4];

2 D Arrays (tables) double [ ][ ] a = new double[3][4];

3 D Arrays

3 D Arrays

3 D Arrays – Arrays of Tables double [ ][ ][ ] year. Expenses

3 D Arrays – Arrays of Tables double [ ][ ][ ] year. Expenses = new double[month][day][expense. Type]; April March February January 13 th 14 th 15 th Gas Food Bus

4 D Arrays and beyond

4 D Arrays and beyond

5 year expense data for IRS double [ ][ ] five. Years. Expenses =

5 year expense data for IRS double [ ][ ] five. Years. Expenses = new double[year][month][day][item]; 20013 20014 20015 20016 20017

4 D Arrays and beyond double [ ][ ] game = new double[game. Level][game.

4 D Arrays and beyond double [ ][ ] game = new double[game. Level][game. Number][row][col]; Beginner's Level Intermediate's Level Expert's Level

How do I work with a portion of that data? Let’s say: only the

How do I work with a portion of that data? Let’s say: only the data from January 2016?

Slicing (N)D arrays into (N-1)D arrays

Slicing (N)D arrays into (N-1)D arrays

Slicing ND arrays into (N-1)D arrays double [ ][ ] five. Year. Expenses =

Slicing ND arrays into (N-1)D arrays double [ ][ ] five. Year. Expenses = new double[5][12][31][4]; // a 4 D array double [ ][ ][ ] year 2016 Expenses = five. Year. Expenses[3]; // a 3 D array double [ ][ ] july 2016 Expenses= year 2016 Expenses[6]; // a 2 D array! double [ ] expenses. On. The 5 Th. Day = july 2016 Expenses[4]; // a 1 D array double gas. Expense. On. The 5 Th. Day = expenses. On. The 5 Th. Day[0]; // a number

double [ ][ ] game = new double[game. Level][game. Number][row][col]; double [ ][ ][

double [ ][ ] game = new double[game. Level][game. Number][row][col]; double [ ][ ][ ] mazes. For. Beginners = game[0]; double [ ][ ] second. Maze. For. Beginners = mazes. For. Beginners [1];

Copying Arrays

Copying Arrays

Copying an array into another array int[] array. A = { 1, 2, 3,

Copying an array into another array int[] array. A = { 1, 2, 3, 4}; System. out. println(array. A); Located at 0 x 3 B memory position 1, 2, 3, 4 int[] array. B = array. A; System. out. println(array. B); array. B located at 0 x 6 A 1, 2, 3, 4 array. B[1] = -1; System. out. println(array. B); System. out. println(array. A); 1, -1, 3, 4 A 1 12 B C D 0 x 3 B 3. 14 0 x 3 F E F G H I 7 A 13 2 T 2 11. 2 53. 1 2 4 9 a W z 6 3 14 1 2 3 4 T h i s 4 ““ i s ““ a ““ l o n 5 g ”“ s t r i n g 6 0 x 3 B B 7 8 Array

Copying/Cloning an array A 1 12 B C D 0 x 3 B 3.

Copying/Cloning an array A 1 12 B C D 0 x 3 B 3. 14 0 x 3 F E F G H I 7 A 13 2 T 2 11. 2 53. 1 2 4 9 a W z 6 3 14 1 2 3 4 T h i s 4 ““ i s ““ a ““ l o n 5 g ”“ s t r i n g 6 0 x 7 B 1 2 3 4 7 8 int[] array. B = new int[array. A. size]; // array. B is a new array // with same size of array. A We can copy the contents of array. A into array. B with a FOR loop later Changing any value of array. B will NOT change the contents of array. A

Adding more numbers to an existing Array

Adding more numbers to an existing Array

Increasing array size A 1 12 B C D 0 x 7 B 0

Increasing array size A 1 12 B C D 0 x 7 B 0 x 3 B 3. 14 0 x 3 F E F G H I 7 A 13 2 T 2 11. 2 53. 1 2 4 9 a W z 6 3 14 1 2 3 4 T h i s 4 ““ i s ““ a ““ l o n 5 g ”“ s t r i n g 6 0 x 7 B 1 2 3 4 9 15 11 7 Can we extend array. A few bites more? 8 int[] array. B = new int[array. A. length + 3]; // array. B is a new array for(…) copy the contents from A to B Add the new values to B array. A = array. B; // put the new reference in array. A