CS 200 - Week 7 Jim Williams, PhD
This Week Exam 1 Results Team Lab: Loops & Eclipse Debugging Lecture: Arrays
Arrays A reference type (not primitive) Hold multiple values Access individual values with an index
Key Array Concepts Variable to hold array reference Allocation of memory for the array Initialization of the array Accessing Array Elements
Which are valid ways to create arrays? 1, 2, 3, 4 1, 2, 4 3, 4 2 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 1, 2, 3 1,3 1,2 none
What will this print? int [] list; list[0] = 10; System.out.print( list[1]); 30 1020 compiler error runtime error
Will these show the contents of the arrays? both will both won't one but not the other 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}; 9 3 compiler error runtime error int sum; int [] list = {1,3,5}; sum = list[0] + list[1] + list[2];
What is the value of z? int [] list = new int[3]; list[1] = 6; int z = list[3]; 3 compiler error runtime error
What is the data type of listG? String array of String compiler error JVM error String [ ] listG = {"first","middle","last"}; Try to draw a memory diagram of this.
Which are true? 1,2,3,4 String []arr = new String[4]; arr[0] = "hello"; arr[1] = "hello"; arr[2] = new String("hello"); arr[3] = arr[2]; 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 1,2,3,4 1,2,4 2,3,4 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; } Yes Not first Not last Error
What does this code do? double value = 0.0; find minimum value find maximum value compiler error logic error 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]; }
Which swaps the ith and i+1th values? temp = list[i]; list[i] = list[i+1]; list[i+1] = temp; temp = list[i+1]; list[i] = temp; list[i+1] = list[i];
What is print out? char [] list1 = new char[]{'a', 'b', 'c'}; true false 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? System.out.println( list1 == list2); true false 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)); java.util.Arrays( list1, list2);
Multi-Dimensional Arrays
Which picture of 2-D array is more accurate? int [][] board = new int[3][2];
How many elements will this array hold? 5 10 25 Error int [][] board = new int[5][5];
What is the data type of board[3][1]? int [][] board = new int[5][5]; int int [] reference to int can't access board[3][1]
What is the data type of board[2]? int [][] board = new int[5][5]; int int [] reference to int can't access board[2]
What values will this array have, initially? boolean [][] board; board = new boolean[5][5]; false true none, they must be initialized first.
Which is correct way to set an element value? int [][] board = new int[5][5]; board[0][0] = 5; board[0,0] = 5; board{0,0} = 5; error
How many elements in this array? int [][] board = {{1,2},{4,5,6}}; 5 6 ragged arrays are not valid error
How would you access "Hi."? responses[1][1][1] Error String [ ][ ][ ] responses = { {{"hello"}, { "How do you do.", "Hi."}}, {{"always"}, { "When?", "Really, always?"}} };
Write code to print out as a grid 1 2 3 4 5 7 8 9 int [][]grid = {{1,2,3},{4,5},{7,8,9}}; for ( int r = 0; r < grid.length; r++) { for ( int c = 0; c < grid[r].length; c++) { System.out.print( grid[r][c]); } System.out.println();
Common Algorithms for Arrays Searching Sorting