9.11. Adding Values to a ListΒΆ

You can add values to a list. If you use add(obj) it will add the passed object to the end of the list. Run the code below to see how the list changes as each object is added.

Note

Notice that we added the same string to the list more than once. Lists can hold duplicate objects.

There are actually two different add methods in the List interface. The add(obj) method adds the passed object to the end of the list. The add(index,obj) method adds the passed object at the passed index, but first moves over any existing values to higher indicies to make room for the new object.

Note

Lists can only hold objects, not primitive values. This means that int values must be wrapped into Integer objects to be stored in a list. You can do this using new Integer(value) as shown above. You can also just put an int value in a list and it will be changed into an Integer object automatically. This is called autoboxing. When you pull an int value out of a list of Integers that is called unboxing.

The code below has the same result as the code above. The compiler will automatically wrap the int values in Integer objects.

Check your understanding

    8-5-1: What will print when the following code executes?

    List<Integer> list1 = new ArrayList<Integer>();
    list1.add(new Integer(1));
    list1.add(new Integer(2));
    list1.add(new Integer(3));
    list1.add(2, new Integer(4));
    list1.add(new Integer(5));
    System.out.println(list1);
    
  • [1, 2, 3, 4, 5]
  • This would be true if all the add method calls were add(value), but at least one is not.
  • [1, 4, 2, 3, 5]
  • This would be true if it was add(1, new Integer(4))
  • [1, 2, 4, 3, 5]
  • The add(2, new Integer(4)) will put the 4 at index 2, but first move the 3 to index 3.
  • [1, 2, 4, 5]
  • This would be true if the add(2, new Integer(4)) replaced what was at index 2, but it actually moves the value currently at index 2 to index 3.

You can step through the code above by clicking on the following Example-8-5-1.

    8-5-2: What will print when the following code executes?

    List<String> list1 = new ArrayList<String>();
    list1.add("Anaya");
    list1.add("Layla");
    list1.add("Sharrie");
    list1.add(1, "Sarah");
    System.out.println(list1);
    
  • ["Anaya", "Sarah", "Layla", "Sharrie"]
  • The add(1, "Sarah") will move any current items to the right and then put "Sarah" at index 1.
  • ["Anaya", "Layla", "Sharrie", "Sarah"]
  • This would be true if the last one was add("Sarah")
  • ["Sarah", "Anaya", "Layla", "Sharrie"]
  • This would be true if the last one was add(0, "Sarah")
  • ["Anaya", "Layla", "Sarah", "Sharrie"]
  • This would be true if the last one was add(2, "Sarah")

You can step through the code above by clicking on the following Example-8-5-2.

    8-5-3: What will print when the following code executes?

    List<Integer> list1 = new ArrayList<Integer>();
    list1.add(5);
    list1.add(4);
    list1.add(3);
    list1.add(1, 2);
    System.out.println(list1);
    
  • [5, 4, 3, 2]
  • Remember that add(obj) adds the object to the end of the list.
  • [5, 4, 1, 3]
  • This would be true if it was add(obj, index), but it is add(index, obj)
  • [2, 5, 4, 3]
  • This would be true if the first index was 1, but it is 0.
  • [5, 2, 4, 3]
  • This adds the 2 to index 1, but first moves all other values past that index to the right.

You can step through the code above by clicking on the following Example-8-5-3.

    8-5-4: What will print when the following code executes?

    List<Integer> list1 = new ArrayList<Integer>();
    list1.add(1);
    list1.add(3);
    list1.add(2);
    list1.add(1);
    System.out.println(list1);
    
  • [1, 3, 2]
  • You can add duplicate objects to a list so this list will have two 1's.
  • [1, 3, 2, 1]
  • The add method adds each object to the end of the list and lists can hold duplicate objects.
  • [1, 1, 2, 3]
  • This would be true if the list was sorted as you add to it, but this is not true.
  • [1, 2, 3]
  • This would be true if the list was sorted and you couldn't add duplicate objects, but lists are not sorted and you can add duplicate objects.
Next Section - 9.12. Getting and Setting Values in a List