1)
ArrayList - Worksheet
What is the output for each of the following? Assume that all problems building the ArrayList start with an empty ArrayList .
Use this declaration for #1 - 5:
ArrayList band = new ArrayList(); band.add(“Lindsey”); band.add(“Mick”); band.add(“Stevie”); band.add(“Danny”); band.set(3, “Christine”);
System.out.println(band);
2) band.add(“Lindsey”); band.add(“Mick”); band.add(“Stevie”); band.add(“Peter”); band.remove(3); band.add(2, “Christine”);
System.out.println(band);
3) band.add(“Lindsey”); band.add(“Mick”); band.add(“Stevie”); band.add(“Dave”); band.add(“Christine”); band.add(band.remove(2)); band.set(0,band.get(4));
System.out.println(band);
4) band.add(“Lindsey”); band.add(“Mick”); band.add(“Stevie”); band.add(“Dave”); band.add(“Christine”); band.remove(2);
Iterator it = band.iterator(); while(it.hasNext())
{
}
System.out.print(it.next() + " ");
1
5) band.add("Lindsey"); band.add("Mick"); band.add("Stevie"); band.add("Danny"); band.add(1,"Christine");
Iterator it = band.listIterator(); while(it.hasNext())
{
}
String name = (String)it.next(); if(name.length() > 6) it.remove();
System.out.println(band);
6) Write a statement in Java that will remove the last element in band and store it in a String named removed.
7) Write a segment of code that would print the members of band backwards.
8) Assume that the ArrayList nums has been initialized with the following Integer objects:
[1, 2, 4, 6, 8, 10, 12] a) Write a statement that would swap the last two elements: b) What will nums contain after the following code has been executed? for(int i= 0; i < nums.size(); i++) nums.remove(i);
2
Consider the following declaration for problems 9-16. Assume that all problems building the ArrayList start with an empty ArrayList.
ArrayList band = new ArrayList();
9. What will band contain if the following code segment is executed? band.add("Paul"); band.add("Pete"); band.add("John"); band.add("George"); band.remove(l);
What will print when this statement is executed?
System.out.println ("Band: " + band);
System.out.println ("Size of the band: " + band.size());
10. What will band contain if the following code segment is executed?
band.add("Paul"); band.add("Pete"); band.add("John"); band.add("George"); band.remove(1); band.add(2,"Ringo");
What will print when this statement is executed?
System.out.println ("Band: " + band);
System.out.println ("Size of the band: " + band.size());
11. What will band contain if the following code segment is executed? band.add("Paul"); band.add("Pete"); band.add("John"); band.add("George") ; band.set(l,"Ringo");
What will print when this statement is executed?
System.out.println ("Band: " + band);
System.out.println ("Size of the band: " + band.size());
12. Which statement will correctly print the second element in the ArrayList?
(A) System.out.println(band.get(l));
(B) System.out.println((String)band.get(l));
(C) System.out.println((String)band.get(2));
(D) System.out.println(band.get(2));
(E) System.out.println(band[l]);
(F) System.out.println(band[2]);
(G) Both (A) and (B)
3
13. What will band contain if the following code segment is executed? band.add("Paul"); band.add("Ringo"); band.add("John"); band.add("George"); band.add(band.remove(O));
What will print when this statement is executed?
System.out.println ("Band: " + band);
System.out.println ("Size of the band: " + band.size());
14. Which statement will correctly remove the last element in band and store it in the variable removed? band.add("Paul"); band.add("Ringo"); band.add("John"); band.add("George");
(A) String removed = (String)band.remove(band.size())
(B) String removed = band.remove(band.size());
(C) String removed = band.remove(band.size()-l);
(D) String removed = (String)band.remove(band.size()-l);
15. Consider the following declaration:
ArrayList recipe = new ArrayList () ;
What will recipe contain if the following code segment is executed?
recipe.add("flour"); recipe.add("baking soda"); recipe.add("butter"); recipe.add("sugar"); recipe.add("brown sugar"); recipe.add("vanilla"); recipe.add("eggs"); recipe.add(2,"salt"); recipe.add("chocolate chips");
16. Which statement will correctly print the
ArrayList recipe?
(A)
System.out.println(recipe);
(B) System.out.println(recipe.toString());
(C) System.out.println(recipe.toString);
(D) System.out.println(recipe());
(E) Both (A) & (B)
4
17. Using the ArrayList recipe as follows:
[flour, baking soda, salt, butter, sugar, brown sugar, vanilla, eggs, chocolate chips]
Which statement will correctly replace "chocolate chips" with "M & M's"?
(A) recipe.set(size(),"M & M's");
(B)
recipe.set(size()-1, " M & M's");
(C)
recipe.set(recipe.size(), "M & M's");
(D)
recipe.set(recipe.size()-1, "M & M's");
18. Using the
ArrayList recipe as follows
:
[flour, baking soda, salt, butter, sugar, brown sugar, vanilla, eggs, chocolate chips]
Which statement will correctly add "nuts" to the end?
(A) recipe.set(recipe.size()-1, "nuts");
(B) recipe.get(recipe.size()-1, "nuts");
(C) recipe.add(remove(size() -1));
(D) recipe.add("nuts");
19. Assume that the
ArrayList primes has been initialized with the following
Integer objects
:
[2, 3, 5, 7, 11, 15, 17, 19]
Which statement will correctly replace the number 15 with the number 13 in the
ArrayList primes?
(A) primes.set(5, "13");
(B) primes.set(6, (Integer)13);
(C) primes.set(5, new Integer(13));
(D) primes.set(5, (Integer)13);
(E) primes.set(6, new Integer(13));
20. Assume that the
ArrayList primes has been initialized with the following
Integer objects.
[2, 3, 5, 7, 11, 13, 17, 19]
What will
primes contain after the following code segment has executed? primes.add(primes.remove(0)) ;
(A) [3, 5, 7, 11, 13, 17, 19]
(B) [3, 5, 7, 11, 13, 17, 19, 2]
(C) [2, 3, 5, 7, 11, 13, 17, 19, 0]
(D) [19, 2, 3, 5, 7, 11, 13, 17]
(E) [19, 3, 5, 7, 11, 13, 17, 2]
5
21. Assume that the ArrayList primes has been initialized with the following Integer objects.
[2, 3, 5, 7, 11, 13, 17, 19]
What will primes contain after the following code segment has executed?
primes.add(0,primes.remove(primes.size()-l));
(A) [3, 5, 7, 11, 13, 17, 19]
(B) [3, 5, 7, 11, 13, 17, 19, 2]
(C) [2, 3, 5, 7, 11, 13, 17, 19, 0]
(D) [19, 2, 3, 5, 7, 11, 13, 17]
(E) [19, 3, 5, 7, 11, 13, 17, 2]
22. Assume that the
ArrayList primes has been initialized with the following
Integer objects.
[2, 3, 5, 7, 11, 13, 17, 19]
Which of the following statements swaps the last two elements?
(A) primes.add(0,primes.remove(primes.size()-l));
(B) primes.add(primes.size()-2,primes.remove(primes.size()-1));
(C) primes.set(primes. size () -2, primes.get(primes.size()-1));
(D) primes.add((primes.size()-l));
(E) primes.add(primes.remove(primes.size()-2));
23. Assume that the
ArrayList primes has been initialized with the following
Integer objects.
[2, 3, 5, 7, 11, 13, 17, 19]
Which of the following represents primes after the following code has been executed? for(int i = 0; i < primes.size(); i++) primes.add(primes.remove(primes.size()-l-i)) ;
(A) [2, 3, 5, 7, 11, 13, 17, 19]
(B) [19, 17, 13, 11, 7, 5, 3, 2]
(C) [3, 5, 7, 11, 13, 17, 19, 2]
(D) [19, 2, 3, 5, 7, 11, 13, 15]
(E) [19, 3, 5, 7, 11, 13, 17, 2]
6
24. Assume that the ArrayList primes has been initialized with the following Integer objects.
[2, 3, 5, 7, 11, 13, 17, 19]
Which of the following represents primes after the following code has been executed? for(int i = 0; i < primes.size(); i++) primes.remove(i) ;
(A) [2, 3, 5, 7, 11, 13, 17, 19]
(B) []
(C) [3, 5, 7, 11, 13, 17, 19]
(D) [2, 5, 11, 17]
(E) [3, 7, 13, 19]
25. Consider the following declaration.
ArrayList numbers = new ArrayList();
Assume numbers contains the following
Integer data:
[-2, 6, 4, -1, 0, 0, 5, 10, -9, 12, -6, 10]
What will numbers contain after the following code segment is executed?
for(int j = 0; j < numbers.size(); j++)
{
Integer tempNum = (Integer) numbers.get(j);
if (tempNum.intValue() < 0) numbers.set(j, new Integer(-l * tempNum.intValue()));
}
7