Download presentation
Presentation is loading. Please wait.
1
CS Week 7 Jim Williams, PhD
2
This Week Exam 1 Results Team Lab: Loops & Eclipse Debugging
Lecture: Arrays
3
Arrays A reference type (not primitive) Hold multiple values
Access individual values with an index
4
Key Array Concepts Variable to hold array reference
Allocation of memory for the array Initialization of the array Accessing Array Elements
5
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
6
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
7
What will this print? int [] list; list[0] = 10;
System.out.print( list[1]); 30 1020 compiler error runtime error
8
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);
9
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];
10
What is the value of z? int [] list = new int[3]; list[1] = 6;
int z = list[3]; 3 compiler error runtime error
11
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.
12
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
13
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
14
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]; }
15
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];
16
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]);
17
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);
18
Multi-Dimensional Arrays
19
Which picture of 2-D array is more accurate?
int [][] board = new int[3][2];
20
How many elements will this array hold?
5 10 25 Error int [][] board = new int[5][5];
21
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]
22
What is the data type of board[2]?
int [][] board = new int[5][5]; int int [] reference to int can't access board[2]
23
What values will this array have, initially?
boolean [][] board; board = new boolean[5][5]; false true none, they must be initialized first.
24
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
25
How many elements in this array?
int [][] board = {{1,2},{4,5,6}}; 5 6 ragged arrays are not valid error
26
How would you access "Hi."? responses[1][1][1]
Error String [ ][ ][ ] responses = { {{"hello"}, { "How do you do.", "Hi."}}, {{"always"}, { "When?", "Really, always?"}} };
27
Write code to print out as a grid
4 5 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();
28
Common Algorithms for Arrays
Searching Sorting
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.