7.12. Code Practice with ArrayLists¶
Fix the following code so that it compiles. The code should instantiate an ArrayList of Strings names
and fill it with the Strings from the array friends
. It should then print out names
.
In line 10, change the terminating condition to i < friends.length
so that you don’t go out of bounds of the array.
This is the answer to the previous question.
Fix the following class so that it will compile and the method reverse
will return an ArrayList containing Integers in the reversed order of the ArrayList parameter list
. Hint: for this solution, only one line needs to be added to the for-loop inside of the reverse
method.
Change line 8 int
to Integer
because ArrayLists only store objects and int is a primitive. Add in line 11 reversed.add(0, element);
so that each element of the ArrayList list
, the parameter, is added in front of the previously added element (thereby reversing the order).
Note that there are other equally correct ways to reverse the order of the ArrayList without creating a new Array or by traversing through it backwards.
This is the answer to the previous question.
Fix the following method printEvenIndex
so that it will print out the Integers at even indices of the passed-in ArrayList list
.
In line 8, the for loop should be written as for (int i = 0; i < list.size(); i++)
so that the method will traverse through all elements of the ArrayList list
. In the line 10 conditional, it should be checking when the index, i
is even - in other words, checking if it is divisible by 2 with no remainder: i % 2 == 0
.
This is the answer to the previous question.
Fix the following method printEvenElements
so that it will print out all of the even Integers that are in the passed-in ArrayList list
.
In line 7, ArrayLists do not have a length property; instead, call the size()
method to find out how long an ArrayList is.
This is the answer to the previous question.
Rewrite the following code so that it fills the ArrayList values
with the elements of the array nums
using a for-each loop instead of a for
loop.
In a for-each loop you specify the type of the values in the array, a name for the current value, and then a :
and then the name of the array. You then want to add
each element to the values
ArrayList.
This is the answer to the previous question.
Finish the following method sumNegVal
to return the sum of all of the negative numbers in the ArrayList list
, the parameter.
Declare a variable to hold the sum
and initialize it to zero. Loop through all the values in the ArrayList. If the current value is negative (less than 0) then add it to the sum
. Return the sum
.
This is the answer to the previous question.
Finish the following method ‘’removeLongStrings’’ that checks each element of the passed in ArrayList list
and removes any that are strictly longer than 4 characters.
Loop through all of the elements of the ArrayList list
. In each iteration, check if the length of each element is strictly greater >
than 4; if it is, remove that element.
This is the answer to the previous question.
Fill in the method shiftLeftOne
below to shift all of the elements of the passed-in ArrayList list
left by one. The original first element should be wrapped to the back of the list after the shift. Ex: {1, 2, 3, 4} should turn turn into {2, 3, 4, 1}
Remove the first element of list
and save it to a new variable of type Integer
. Because of the nature of remove, everything else in the ArrayList will shift left accordingly. The only thing left to do after that is add this value to the back of the ArrayList.
This is the answer to the previous question.
Finish the method moveSmallest
so that it finds the smallest value in the passed-in ArrayList list
and moves it to the front of the list.
Fill-in the for loop so that it will traverse through the entire ArrayList. The conditional should check if the current element at index i
is less than the element at smallestIndex
. After the for-loop has completed, the method must remove the value at smallestIndex
and save it to a variable, and then add it to the front of the ArrayList
This is the answer to the previous question.
Finish the method findLongest
to find and return the longest String in the ArrayList of Strings list
.
Declare a variable to hold the longest
String. Initialize it to the empty string ""
. Loop through all the values in the ArrayList and compare its length to the length of longest
. Return longest
.
This is the answer to the previous question.