This book is now obsolete Please use CSAwesome instead.
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
- [1, 2, 3, 4, 5]
- This would be true if all the
add
method calls wereadd(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.
8-5-4: 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);
You can step through the code above by clicking on the following Example-8-5-1.
- ["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")
8-5-5: 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);
You can step through the code above by clicking on the following Example-8-5-2.
- [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 isadd(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.
8-5-6: 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);
You can step through the code above by clicking on the following Example-8-5-3.
- [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.
8-5-7: 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);