360 likes | 385 Views
CS 200 Arrays. Jim Williams, PhD. This Week. Team Lab: Loops & Debugging P6 Due Thursday BP1 Milestone 1 available this week. Exam Review Come to Marc's or my office hours to review Exam Final Exam policy Lecture: Arrays. MasterMind Demo. Milestone Bonus Points.
E N D
CS 200 Arrays Jim Williams, PhD
This Week • Team Lab: Loops & Debugging • P6 Due Thursday • BP1 Milestone 1 available this week. • Exam Review • Come to Marc's or my office hours to review Exam • Final Exam policy • Lecture: Arrays
MasterMind Demo Milestone Bonus Points
Changing Behavior of Loop continue - skip rest of body, check condition break - leave loop immediately Affect only the innermost loop in which they are contained.
What is the output? for (int i = 1; i <= 5; ++i) { System.out.print("\n" + i + " : "); for (int j = i; j <= 10; j++) { if (i % 2 == 0) continue; if (j % 3 == 0) break; System.out.print(j + " "); } }
Data Structures Organize data to suit a specific purpose. CS 300 OO Programming and Basic Data Structures CS 400 Advanced Data Structures and SW Carpentry
Arrays • Hold multiple values • Access individual values with an index • A reference type (not primitive) • Automatically initialized (allocated on heap) • Contiguous memory area
Memory Diagram for Arrays in main method: int [] list; list = new int[5]; int [] list2 = new int[]{ 2, 4, 6, 8}; terms: element, index, element datatype reference, braces { }, brackets [ ]
Today P6 is due today BP1 is now available • milestone 1 due Thursday, March 14th. Lecture: Single and multi-dimensional arrays (arrays and arrays of arrays)
Why array indexing starts with 0. • Single dimension arrays are contiguous in memory • Equally efficient to access any element • nth element's address is: address + (n * elementSize)
int [] list3 = new int[]{ 3, 5, 7, 9}; System.out.println( list3[3] );
Question double [] list = new double[]{8.0,7.1,6.2,5.3,4.4}; If the array begins at address 1000, what is address of the element with value 6.2?
Which are valid ways to create arrays? int [] list1; //1 int [] list2 = new int[5]; //2 int list3 = new int[]{1,2,3}; //3 int [] list4 = new int[1,2,3,4];//4
Which array initializations are valid? int [] listA = new int[4]; //1 int [] listB = new int[]{8, 7, 6}; //2 Scanner input = new Scanner( System.in); int sizeC = input.nextInt(); int [] listC = new int[ sizeC]; //3
What will this print? int [] list = null; list[0] = 10; list[1] = list[0] + 20; System.out.print( list[1]);
Will these show the contents of the arrays? char [] chars = {'a','b','c','d','e'}; System.out.println(chars); int [] list = {10, 2, 23, 64}; System.out.println(list);
What is the value of sum? int sum; int [] list = {1,3,5}; sum = list[0] + list[1] + list[2];
Either syntax works in Java int [ ] list1; //preferable OR int list2 [ ];
What is the value of z? int [] list = new int[3]; list[1] = 6; list[2] = 3; int z = list[3];
What is the data type of listG? String [ ] listG = {"first","middle","last"}; Try to draw a memory diagram of this.
Which are true? String []arr = new String[4]; arr[2] = new String("hello"); arr[3] = arr[2]; arr[0] = "hello"; arr[1] = "hello"; System.out.println( arr[2] == "hello" ); //1 System.out.println( arr[2].equals( "hello")); //2 System.out.println( arr[3] == arr[2]); //3 System.out.println( arr[3].equals(arr[2])); //4
Will this set all elements to 3? final int START_VALUE = 3; int [] list = new int[5]; for ( int i = 0; i < list.length -1; i++) { list[i+1] = START_VALUE; }
What does this code do? double value = 0.0; double [] nums= {3.0,5.0,2.3,4.1}; for ( int i = nums.length; i > 0; i--) { if ( nums[i-1] > value) { value = nums[i-1]; } }
What is print out? char [] list1 = new char[]{'a', 'b', 'c'}; char [] list2 = new char[]{'b', 'b', 'c'}; System.out.println( list1.length == list2.length); System.out.println( list1[1] == list2[1]);
What is print out? char [] list1 = new char[]{'a', 'b', 'c'}; char [] list2 = new char[]{'a', 'b', 'c'}; System.out.println( list1 == list2); System.out.println( list1.equals( list2)); See java.util.Arrays for equals methods to compare the contents of arrays.
Multi-Dimensional Arrays More accurately: multiple single dimensional arrays
Which picture of 2-D array is more accurate? int [][] board = new int[3][2];
How many elements will this array hold? int [][] board = new int[5][5];
What is the data type of board[3][1]? int [][] board = new int[5][5];
What is the data type of board[2]? int [][] board = new int[5][5];
What values will this array have, initially? boolean [][] board; board = new boolean[5][5];
Which is correct way to set an element value? int [][] board = new int[5][5];
How many elements in this array? int [][] board = {{1,2},{4,5,6}};
How would you access "Hi."? String [ ][ ][ ] responses = { {{"hello"}, { "How do you do.", "Hi."}}, {{"always"}, { "When?", "Really, always?"}} };