1.
Q1: Put the code in the appropriate order so that an
ArrayList
of integers will store multiples of 5 (from 100 down to 0) in reverse order.
ArrayList<DataType> name;
new
keyword with the constructor to create a new ArrayList object (When ArrayLists are instantiated, they are empty and have a size of 0.)
listName.add(valueToBeAdded)
listName.add(index, valueToBeAdded)
where index is within the bounds 0 to listName.size()
listName.get(index)
to retrieve the element
listName.size() - 1
, otherwise an IndexOutOfBoundsException occurs
listName.set(index, newValue)
to update the value
listName.size() - 1
, otherwise an IndexOutOfBoundsException occurs
for (DataType item : listName)
- iterates from first to last, storing a copy of each element in item
listName.size() - 1
for reverse)
ArrayList
of integers will store multiples of 5 (from 100 down to 0) in reverse order.
ArrayList
named numbers
. Which of the following code snippets would be best for this task?
public void addOne(ArrayList<Integer> numbers) {
for (int i = 0; i < numbers.size(); i++) {
numbers.set(i, numbers.get(i) + 1);
}
}
public void addOne(ArrayList<Integer> numbers) {
for (int num : numbers) {
num++;
}
}
public void addOne(ArrayList<Integer> numbers) {
for (int num : numbers) {
numbers.set(num, num + 1);
}
}
public void addOne(ArrayList<Integer> numbers) {
for (int i = 0; i < numbers.size(); i++) {
numbers.set(i, i + 1);
}
}