What is the first index of every Array/ArrayList?
0
When is a for-each loop not an appropriate traversal method for iterating through an array?
When the goal is to edit the array
When it is not appropriate to look at each element.
System.out.println(Arrays.toString(your array name));
for(int x = 0; x < i.length; x++) { i[x] = x; }
Should print [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Write the code to create an ArrayList of decimal numbers with the name "gpas"
ArrayList<Double> gpas = new ArrayList<Double>();
Write the following for loop as a for each loop!
ArrayList <Integer> a = new ArrayList<Integer>()
biggest = a.get(0);
for (x=1;x < a.size(); x++){
if (biggest < a.get(x)) {biggest = a.get(x)}
}
return biggest
ArrayList <Integer> a = new ArrayList<Integer>()
biggest = a.get(0);
for (Integer element: a){
if (biggest < element) {biggest = element}
}
return biggest
Write the code to change the value of the 3rd element of the following array to 98.
int[] grades = {100, 92, 70, 88, 78}
grades[2] = 98;
Write the method/variable that returns the length of each of the following:
String
Array
ArrayList
String: .length()
Array: .length
ArrayList: .size()
Write the code for a loop that adds 5 random values from 1-6 into an ArrayList called "diceRolls". Print out those values.
Answers may vary:
ArrayList<Integer> diceRolls = new ArrayList<Integer>();
int c = 5;
while(c-->0) { diceRolls.add((int) (Math.random() * 6) + 1); }
For each of the following write what types of values they can have:
Arrays
2D Arrays
ArrayLists
Array: primitive or objects
2D Arrays: Arrays
ArrayLists: objects
String s = "Happy Days";System.out.println() + s.substring(5,6));s.substring(0, 1) + s.substring(2,3
Hp_
String[][] letters = {{"A", "B", "C"},{"D", "E", "F"},{"G", "H", "I"}};
System.out.println( /* missing code */ );
What code would have the program display "CAGE"?
letters[0][2] + letters[0][0] + letters[2][0] + letters[1][1]
How do you remove an element from an array? (Not set it blank)
Traverse the old array and add the old elements into a new array with 1 smaller size skipping the element that needed to be removed
What is printed from the following code:
List<String> students = new ArrayList<String>();students.add("Bob");students.add("George");students.add("Edward");for(String s : students) { System.out.println(s); }
Bob
George
Edward
What is a for-each (enhanced for loop)? What does it look like?
A for loop with 2 parameters that always loops through all the elements. There is no conditional statement or counter variable.
for(Object o : list) { /* Do Something */ }
Consider the following code:for (int num = 0; num < 10; num += 2) {}
How many times will num < 10 be checked?
6
Assume that ArrayList nums has the following values: 0, 1, 2, 3, 4
What is printed from the following code:System.out.println(nums[3] + nums[2] + nums[1]);
Compile Time Error, array required but ArrayList found, wrong types
Assume an ArrayList<Integer> arr has 10 values. Write the code to swap the 3rd and 7th values.
int tmp = arr.set(7, arr.get(2));
arr.set(2, tmp);
How many values would be updated before the following code errors?int[][] anArray = new int[8][5];
for (int j = 0; j < 5; j++)for (int k = 0; k < 8; k++) anArray[j][k] = 25;
5
What is the name of the error you get when trying to access an array element that does not exist?
ArrayIndexOutOfBoundsException
What is printed from the following code:
String x = new String("20");
String y = new String("20");System.out.println(x.equals(y));System.out.println(x == y);
true
false
Assume that ArrayList nums has the following values: "z", "o", "tw", "th", "f"
What is printed from the following code:
nums.set(3, "4")
nums.remove(2)
nums.set(1, nums.get(3))
System.out.println(nums);
["f","o","4","f"]
Write the code or pseudo code to get the last element of a 2D array, arr
arr[arr.length - 1][arr[arr.length - 1].length - 1]
for (int num = 0; num < 5; num ++) { int tmp = num;
for (; num < 10; num++) { System.out.println("hop"); } num = tmp;
}
How many times will System.out.println("hop") be executed?
40
How do you change the size of an array? (the answer is not you can't)
Create a new array and assign it to the old variable
Consider the following code:int x = (int)(Math.random() * 10) + 20;
int y = (int)(Math.random() * 20) + 10;
What possible values could both x and y share? (Compare the ranges and find the numbers that are the same)
x can be 20-29, y can be 10-29 so they can both be 20-29