1.
Q1: Put the code in the appropriate order so that an array of integers will store multiples of 5 (0 to 100) in reverse order.
arrayName[index]
returns value stored at that index
(arrayName.length-1)
, inclusive otherwise an IndexOutOfBounds exception occurs at runtime
for (DataType varName : collectionName)
- traverses collectionName by iterating from first element to last element storing a copy of each element from collectionName in varName.
arrayName.length - 1
) to go backwards
arrayName.length
for forwards, loop control variable >= 0 for backwards
public void addOne(int[] numbers) {
for (int index = 0; index < numbers.length; index++ ) {
numbers[index] = numbers + 1;
}
}
public void addOne(int[] numbers) {
for (int num: numbers) {
num++;
}
}
public void addOne(int[] numbers) {
for (int num: numbers) {
numbers[num] = numbers[num] + 1;
}
}
public void addOne(int[] numbers) {
for (int index = 0; index < numbers.length; index++) {
numbers[index] = index + 1;
}
}