Java Chapter 7
String length vs array length
Array-field: names.length; String-method: name.length();
ArrayList diamond operator
ArrayList<String> list=new ArrayList<>();
ArrayList designate a different capacity
ArrayList<String> list=new ArrayList<String>(100);
creating an ArrayList
ArrayList<String> nameList = new ArrayList<String>();
Which of the following is a correct method header for receiving a two-dimensional array as an argument? A. public static void passArray(int[2]) B. public static void passArray(int [][]) C. public static void passArray(int[1][2]) D. public static void passArray(int[], int[])
B. public static void passArray(int [][])
this is the typical number of comparisons performed by the sequential search on an array of N elements (assuming the search values are consistently found)
N/2
ArrayList toString method
System.out.println(nameList); output: [ James ]
Java does not allow a statement to use a subscript that is outside the range of valid subscripts for an array. T or F
True
volume of a cylinder
V=πr²h
What is the representation of the third element in an array called a?
a[2]
this array field holds the number of elements that the array has
length
2D array length field
length field gives the number of rows each row has a length constant that tells how many columns is in that row
An array's size declarator can be a negative integer expression T or F
false
The first size declarator in the declaration of a two- dimensional array represents the number of columns. The second size declarator represents the number of rows. T or F
false
ArrayList add method
nameList.add("James");
ArrayList modified add method
nameList.add(1, "Mary");
ArrayList get method
nameList.get(1); gets name at index 1
ArrayList remove method
nameList.remove(1);
ArrayList set method
nameList.set(1, "Becky"); replace existing item
ArrayList size method
nameList.size();
A two-dimensional array had multiple length fields T or F
true
An ArrayList automatically expands in size to accommodate the items stored in it. T or F
true
The Java compiler does not display an error message when it processes a statement that uses an invalid subscript T or F
true
The subscript of the last element in a single-dimensional array is one less than the total number of elements in the array T or F
true
both of the following declarations are legal and equivalent: int[] numbers; int numbers[]; T or F
true
the values in an initialization list are stored in the array in the order that they appear in the list T or F
true
when an array is passed to a method, the method has access to the original array T or F
true
array bounds checking happens a. when the program is compiled b. when the program is saved c. when the program runs d. when the program is loaded into memory
when the program runs
ragged array
when the rows of a 2D array are different lengths int[][] ragged=new int [4][]; then create columns: ragged[0]=new int[3]; ragged[1]=new int[4]; etc.
Suppose array a is int[] a = {1, 2, 3}, what is a[0] - a[2]?
-2
Suppose: int[] numbers = new int[10]; What is numbers[0]?
0
the first subscript in an array is always (#)
0
the last subscript in an array is always
1 less than the number of elements
ArrayList default capacity
10
Show the output of the following code: public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4, 5}; increase(x); int[] y = {1, 2, 3, 4, 5}; increase(y[0]); System.out.println(x[0] + " " + y[0]);} public static void increase(int[] x) { for (int i = 0; i < x.length; i++) x[i]++;} public static void increase(int y) { y++;}}
2 1
In the following code, what is the printout for list2? class Test { public static void main(String[] args) { int[] list1 = {3, 2, 1}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = list2.length - 1; i >= 0; i--) System.out.print(list2[i] + " ");}}
2 1 0
If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is _____
2.0
Surface Area of a Cylinder
2πrh + 2πr^2
What will be the value of x[1] after the following code is executed? int[] x = {22, 33, 44}; arrayProcess(x[1]); ... public static void arrayProcess(int a){ a = a + 5;}
33
What will be the value of x[1] after the following code is executed? int[] x = {22, 33, 44}; arrayProcess(x); ... public static void arrayProcess(int[] a){ for(int k = 0; k < 3; k++){ a[k] = a[k] + 5;}}
38
Assume int[] t = {1, 2, 3, 4}. What is t.length?
4
Assume double[][] x = new double[4][5], what are x.length and x[2].length?
4 and 5
What will be the result of executing the following code? int[] x = {0, 1, 2, 3, 4, 5}; A. An array of 6 values ranging from 0 through 5 and referenced by the variable x will be created. B. A compilation error will occur. C. The program will crash when it is executed. D. The value of x[1] will be 0, x[2] will be 0, x[3] will be 0, x[4] will be 0, x[5] will be 0, and x[6] will be 0.
A. An array of 6 values ranging from 0 through 5 and referenced by the variable x will be created.
What would be the results of the following code? int[] array1 = new int[25]; ... // Code that will put values in array1 int value = array1[0]; for (int a = 1; a < array1.length; a++){ if (array1[a] < value) value = array1[a];} A. value contains the highest value in array1 B. value contains the lowest value in array1 C. value contains the sum of all the values in array1 D. value contains the average of the values in array1
B. value contains the lowest value in array1
For the following code, what would be the value of str[2]? String[] str = {"abc", "def", "ghi", "jkl"}; A. "ghi" B. "def" C. A reference to the String "ghi" D. A reference to the String "def"
C. A reference to the String "ghi"
When you pass an array to a method, the method receives _____. A. a copy of the array B. a copy of the first element C. the reference of the array D. the length of the array
C. the reference of the array
Given the following declaration: nt[ ][ ] m = new int[5][6]; Which of the following statements is true? A. m[2][4] represents the element stored in the 2nd row and the 4th column of m. B. m.length has the value 6. C. m[0].length has the value 5. D. The name m represents a two-dimensional array of 30 int values.
D. The name m represents a two-dimensional array of 30 int values.
When an array is passed to a method _____. A. a reference to the array is passed B. it is passed just as an object C. the method has direct access to the original array D. all of the above
D. all of the above
If numbers is a two-dimensional int array that has been initialized and total is an int that has been set to 0, which of the following will sum all the elements in the array? A. for (int row = 1; row < numbers.length; row++){for (int col = 1; col < numbers.length; col++)total += numbers[row][col];} B. for (int row = 0; row < numbers.length; row++){for (int col = 0; col < numbers.length; col++)total += numbers[row][col];} C. for (int row = 0; row < numbers[row].length; row++){for (int col = 0; col < numbers.length; col++)total += numbers[row][col];} D. for (int row = 0; row < numbers.length; row++){for (int col = 0; col < numbers[row].length; col++)total += numbers[row][col];}
D. for (int row = 0; row < numbers.length; row++){for (int col = 0; col < numbers[row].length; col++)total += numbers[row][col];}
Analyze the following code: public class Test { public static void main(String[] args) { int[] oldList = {1, 2, 3, 4, 5}; reverse(oldList); for (int i = 0; i < oldList.length; i++) System.out.print(oldList[i] + " ");} public static void reverse(int[] list) { int[] newList = new int[list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i]; list = newList;}}
The program displays 1 2 3 4 5.
to insert an item at a specific location in an ArrayList object, you use this method
add
Array List definition and import statement
automatically expands and shrinks import java.util.ArrayList;
This search algorithm repeatedly divides the portion of an array being searched in half.
binary search
When initializing a two-dimensional array, you enclose each row's initialization list in
braces
2D arrays
double[][]scores=new double [3][4]; 1st is rows, second is columns
enhanced for loop
goes through all elements for(datatype elementVariable : array) statement;
String arrays
if an initialization list is not provided then the new keyword must be used to create the array: String[] names=new String[4]; then each element must be initialized: names[0]="Bill"; names[1]="Susan"; etc.
create an array
int[] numbers=new int[6]; //Create an array that holds 6 integers
let user specify array size
int[] tests; Scanner keyboard=new Scanner(System.in); System.out.print("How many tests?"); int numTests=keyboard.nextInt(); tests=new int[numTests];
to delete an item from an ArrayList object, you use this method
remove
binary search
requires the array to be sorted in ascending order, starts in the middle, then will look in the first half or last half of the array depending on the desired value
Access a 2D array element
scores[2][1]=95;
this search algorithm steps through an array, comparing each item with the search value
sequential search
to determine the number of items stored in an ArrayList object, you use this method
size
In an array declaration, this indicates the number of elements that the array will have.
size declarator
selection sort
smallest value is moved to element 0, next smallest to element 1 and so on
Each element of an array is accessed by a number known as a( n) __________.
subscript
Do the following two programs produce the same result? Program I: public class Test { public static void main(String[] args) { int[] list = {1, 2, 3, 4, 5}; reverse(list); for (int i = 0; i < list.length; i++) System.out.print(list[i] + " ");} public static void reverse(int[] list) { int[] newList = new int[list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i]; list = newList;}} Program II: public class Test { public static void main(String[] args) { int[] oldList = {1, 2, 3, 4, 5}; reverse(oldList); for (int i = 0; i < oldList.length; i++) System.out.print(oldList[i] + " ");} public static void reverse(int[] list) { int[] newList = new int[list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[list.length - 1 - i]; list = newList;}}
yes