Download presentation
Presentation is loading. Please wait.
1
CS 200 Arrays Jim Williams, PhD
2
This Week Team Lab: Loops & Debugging Exam Review
Come to office hours to review in detail Big Program 1: Sokoban Lecture: Arrays
3
Arrays Hold multiple values Access individual values with an index
A reference type (not primitive) Automatically initialized (allocated on heap) Contiguous memory area
4
Memory Diagram for Arrays
int [] list; list = new int[5]; int [] list2 = new int[]{ 2, 4, 6, 8}; terms: element, index, element datatype reference, braces { }, brackets [ ]
5
Where is element n? single dimension arrays are contiguous in memory
Equally efficient to access any element nth element's location: address + (n * elementSize) Helpful that arrays start with 0. Why were the 1900's the 20th century? Because centuries started counting at 1 rather than 0.
6
Question int [] list = new int[]{8,7,6,5,4};
If array begins at address 1000, what is address of the element with value 6? * 2 is (4 is size of int in bytes, 2 is index of value 6)
7
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 try it.
8
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 try it.
9
What will this print? int [] list = null; list[0] = 10;
list[1] = list[0] + 20; System.out.print( list[1]); 30 1020 compiler error runtime error try it.
10
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);
11
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]; try it.
12
Today P6 Due today BP1 Milestone 1 due next Thursday Lecture: Arrays
13
Either syntax works in Java
int [ ] list1; //preferable OR int list2 [ ];
14
What is the value of z? int [] list = new int[3]; list[1] = 6;
int z = list[3]; 3 compiler error runtime error try it
15
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. Answer: array of String String is the element data type
16
Which are true? 1,2,3,4 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 1,2,3,4 1,2,4 2,3,4 2,4 try it
17
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 try it
18
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]; } try it.
19
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]; try it
20
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]); try it.
21
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)); try it. Note: equals method depends on which equals method is called. This equals is probably the one inherited from Object and simply compares references See java.util.Arrays for equals methods to compare the contents of arrays. See java.util.Arrays for equals methods to compare the contents of arrays.
22
Multi-Dimensional Arrays
More accurately: multiple single dimensional arrays
23
Which picture of 2-D array is more accurate?
int [][] board = new int[3][2]; Depends on your purpose. The first is simpler conceptually and for many purposes, fine. However, when working with code the 2nd is more technically accurate and important to understand.
24
How many elements will this array hold?
5 10 25 Error int [][] board = new int[5][5]; try it.
25
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] int element data type
26
What is the data type of board[2]?
int [][] board = new int[5][5]; int int [] reference to int can't access board[2] int [] The declaration is: int [][] board. So board dereferenced once (board[2]) results in: int []
27
What values will this array have, initially?
boolean [][] board; board = new boolean[5][5]; false true none, they must be initialized first. try it.
28
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 try it.
29
How many elements in this array?
int [][] board = {{1,2},{4,5,6}}; 5 6 ragged arrays are not valid error try it.
30
How would you access "Hi."? responses[1][1][1]
Error String [ ][ ][ ] responses = { {{"hello"}, { "How do you do.", "Hi."}}, {{"always"}, { "When?", "Really, always?"}} }; try it.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.