Looping Through a ListΒΆ

You can use a for-each loop to loop through all items of a list, just like you do with an array.

private List<String> nameList;

public boolean findName(String name)
{
   for (String currName: nameList)
   {
      if (currName.equals(name)) return true;
   }
   return false;
}

The code above loops through the nameList looking for the passed name. If it is found it returns true, otherwise it will return false. Note the use of the equals method to compare strings. This is the correct way to check if two strings have the same characters in the same order.

Be careful when you remove items from a list as you loop through it. Remember that removing an item from a list will shift the remaining items to the left.

private List<String> nameList;

public boolean removeName(String name)
{
   boolean found = false;
   int index = 0;
   while (index < nameList.size())
   {
      if (name.equals(nameList.get(index))
      {
         remove(index);
         found = true;
      }
      else index++;
   }
   return found;
}

Notice that the method above only increments the current index if an item was removed from the list. If you increment the index in all cases you will miss checking some of the elements since the items shift left.

8-4-1: Assume that nums has been created as an ArrayList object and it initially contains the following Integer values. [0, 0, 4, 2, 5, 0, 3, 0] What will nums contain as a result of executing numQuest?

List<Integer> list1 = new ArrayList<Integer>();
private List<Integer> nums;

// precondition: nums.size() > 0;
// nums contains Integer objects
public void numQuest()
{
   int k = 0;
   Integer zero = new Integer(0);
   while (k < nums.size())
   {
      if (nums.get(k).equals(zero))
         nums.remove(k);
      k++;
   }
}





8-4-2: Which of the following is a reason to use a list (assume an object of the class ArrayList) instead of an array?





Mixed up programs

8-4-3: The following has the correct code for the method getScore plus at least one extra unneeded code statement. This method will calculate and return the score for a word game. The code should loop through all of the elements in wordList and if the length of the current word is 3 it should add one to the score, if the length of the word is 4 it should add 2 to the score, and if the length is greater than 4 it should add 3 to the score. The method should return the score. Drag the needed blocks from the left into the correct order on the right. Check your solution by clicking on the Check Me button. You will be told if any of the blocks are in the wrong order or if you need to remove one or more blocks.

8-4-4: The following has the correct code for a method called insertInOrder plus at least one extra unneeded code statement. This method should add the passed name in alphabetic order to a private list field called nameList. Drag the needed blocks from the left into the correct order on the right. Check your solution by clicking on the Check Me button. You will be told if any of the blocks are in the wrong order or if you need to remove one or more blocks.