ch02 - 1.1 Describe the effect of each of the following...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1.1 Describe the effect of each of the following operations on object myList as shown at the bottom of Figure 2.2. What is the value of myList.size() after each operation? myList.add("Pokey"); myList.add("Campy"); int i = myList.indexOf("Happy"); myList.set(i, "Bouncy"); myList.remove(myList.size() - 2); String temp = myList.get(1); myList.set(1, temp.toUpperCase()); start size 6 myList.add("Pokey"); adds element to end of list, size now at 7 myList.add("Campy"); adds element to end of list, size now 8 int i = myList.indexOf("Happy"); returns integer 4 to i myList.set(i, "Bouncy"); sets element i (4) to "Bouncy". Size = 8 myList.remove(myList.size() - 2); Removes item 6 ("Pokey"). Size = 7 String temp = (String) myList.get(1); Stores "Awful" in temp. Size = 7 myList.set(1, temp.toUpperCase()); Sets item 1 to "AWFUL", replacing "Awful". Final size = 7 2.1 What does the following code fragment do? ArrayList<Double> myList = new ArrayList<Double>(); myList.add(3.456); myList.add(5.0); double result = myList.get(0) + myList.get(1); System.out.println("Result is " + result); ArrayList<Double> myList = new ArrayList<Double>(); Initialized myList to an empty ArrayList<Double> myList.add(3.456); Appends the value 3.456 to the end of the list myList.add(5.0); Appends the value 5.0 to the end of the list double result = myList.get(0) + myList.get(1); Sets result to 3.456 + 5.0 System.out.println("Result is " + result); Outputs Result is 8.456 3.1 Trace the execution of the following: int[] anArray = {0, 1, 2, 3, 4, 5, 6, 7}; for (int i = 3; i < anArray.length – 1; i++) anArray[i + 1] = anArray[i];
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
and the following: int[] anArray = {0, 1, 2, 3, 4, 5, 6, 7}; for (int i = anArray.length – 1; i > 3; i--) anArray[i] = anArray[i – 1]; What are the contents of anArray after the execution of each loop? int[] anArray = {0, 1, 2, 3, 4, 5, 6, 7}; anArray [0] [1] [2] [3] [4] [5] [6] [7] 0 1 2 3 4 5 6 7 for (int i = 3; i < anArray.length – 1; i++) i = 3 anArray[i + 1] = anArray[i]; anArray [0] [1] [2] [3] [4] [5] [6] [7] 0 1 2 3 3 5 6 7 for (int i = 3; i < anArray.length – 1; i++) i = 4 anArray[i + 1] = anArray[i]; anArray [0] [1] [2] [3] [4] [5] [6] [7] 0 1 2 3 3 3 6 7 for (int i = 3; i < anArray.length – 1; i++) i = 5 anArray[i + 1] = anArray[i]; anArray [0] [1] [2] [3] [4] [5] [6] [7] 0 1 2 3 3 3 3 7 for (int i = 3; i < anArray.length – 1; i++) i = 6 anArray[i + 1] = anArray[i]; anArray [0] [1] [2] [3] [4] [5] [6] [7] 0 1 2 3 3 3 3 3 for (int i = 3; i < anArray.length – 1; i++) i = 6 Loop exits int[] anArray = {0, 1, 2, 3, 4, 5, 6, 7}; anArray [0] [1] [2] [3] [4] [5] [6] [7] 0 1 2 3 4 5 6 7 for (int i = anArray.length-1; i > 3; i--) i = 7 anArray[i] = anArray[i-1]; anArray [0] [1] [2] [3] [4] [5] [6] [7] 0 1 2 3 4 5 6 6
Background image of page 2
for (int i = anArray.length-1; i > 3; i--) i = 6 anArray[i] = anArray[i-1]; anArray [0] [1] [2] [3] [4] [5] [6] [7] 0 1 2 3 4 5 5 6 for (int i = anArray.length-1; i > 3; i--) i = 5 anArray[i] = anArray[i-1]; anArray [0] [1] [2] [3] [4] [5] [6] [7] 0 1 2 3 4 4 5 6 for (int i = anArray.length-1; i > 3; i--) i = 4 anArray[i] = anArray[i-1]; anArray [0] [1] [2] [3] [4] [5] [6] [7] 0 1 2 3 3 4 5 6 for (int i = anArray.length-1; i > 3; i--) i = 3 Loop exits 3.2 Write statements to remove the middle object from a KWArrayList and place it at the end.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 11/01/2011 for the course COMPSCI 225 at Boise State.

Page1 / 12

ch02 - 1.1 Describe the effect of each of the following...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online