Java Chapter 7 - Arrays & Array Lists
traversing all elements in an array
What is the enhanced for loop convenient for?
int[] numarray = new int[10];
a statement for defining an integer array named numarray of ten elements
maintain a companion variable that stores the current number of elements
an array myArray is only partially filled, to keep track of the current number of elements you would...
1
consider the following code snippet: ArrayList<Double> somedata = new ArrayList<Double>(); somedata.add(10.5); the size of the array list somedata after the given code snippet is executed is:
5
consider the following code snippet: int cnt = 0; int[][] numarray = new int[2][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { numarray[j][i] = cnt; cnt++; } } the value of numarray[1][2] after the code snippet is executed is:
System.out.println(arr[1][1]);
consider the following code snippet: int[][] arr = { { 13, 23, 33 }, { 14, 24, 34 } }; the appropriate statement that displays the value 24 is:
Arrays.sort(data);
considering the following code snippet: String[] data = { "123", "ghi", "jkl", "def", "%&*" }; the statement that sorts the data array in ascending order
for (String str : arr)
the correct enhanced for loop to iterate over the array without using an index variable (fill in the blank line). String[] arr = { "abc", "def", "ghi", "jkl" }; _________ { System.out.print(str); }
methods that return an array cannot specify its size
the method header public static int[5] meth(int[] arr, int[] num) is invalid because:
30
the number of elements that can be stored in a two-dimensional 5 by 6 array
6
the number of elements that can be stored in an array of dimension 2 by 3
6
the output of the code snippet below is: int[][] arr = { { 1, 2, 3, 0 }, { 4, 5, 6, 0 }, { 0, 0, 0, 0 } }; int val = arr[1][2] + arr[1][3]; System.out.println(val);
3040
the output of the code snippet: int[] myarray = { 10, 20, 30, 40, 50 }; System.out.print(myarray[2]); System.out.print(myarray[3]);
2345
the output of the given code snippet? int[] mynum = new int[5]; for (int i = 1; i < 5; i++) { mynum[i] = i + 1; System.out.print(mynum[i]); }
aaa 3
the output of this code snippet is: public static void main(String[] args) { String[] arr = { "aaa", "bbb", "ccc" }; mystery(arr); System.out.println(arr[0] + " " + arr.length); } public static void mystery(String[] arr) { arr = new String[5]; arr[0] = "ddd"; }
code snippet causes a bounds error
the result of executing this code snippet: int[] marks = { 90, 45, 67 }; for (int i = 0; i <= 3; i++) { System.out.println(marks[i]); }
the array values is not modified
the result of the following code: for (double element : values) { element = 0; }
many people feel it is less intimidating than drawing diagrams
the use of physical objects helpful in algorithm design because:
1
the value of the count variable after the execution of the given code snippet: ArrayList<Integer> num = new ArrayList<Integer>(); num.add(1); num.add(2); num.add(1); int count = 0; for (int i = 0; i < num.size(); i++) { if (num.get(i) % 2 == 0) { count++; } }
take the length of the array minus the number of elements currently in the array
to determine the number of slots in a partially filled array, that are not currently used you would...
remove an element
to process an array of values and eliminate any potential duplicate values stored in the array, the array algorithm that might be adapted for this us:
1 86 24 4
what will be printed by the statements below is: int[] values = { 1, 2, 3, 4}; values[2] = 24; values[values[0]] = 86; for (int i = 0; i < values.length; i++) System.out.print (values[i] + " ");
2
what will be printed by the statements below is: int[] values = { 10, 24, 3, 64}; int position = 0; for (int i = 1; i < values.length; i++) if (values[i] > values[position]) position = i; System.out.print (position);
the program will not compile
what will happen if you confuse the method required for checking the length of a string and using size() instead of length(), is:
the array list contains at least one element
what you should check for when calculating the smallest value in an array list is:
auto-boxing
when an integer literal is added to an array list declared as ArrayList<Integer>, the action that Java performs is:
when the elements of the array are ordered
when is the binary search faster than the linear search?
the number of inputs may not be known in advance
when reading inputs, why might it be necessary to "grow" an array because:
to replace the element to be deleted with the last element in the array
when the order of the elements is unimportant, the most efficient way to remove an element from an array is:
it stays the same
when two cards in a deck of cards shuffle their position what happens to the size of the array is: