9.11. Adding Values To A List — AP CSA Java Review - Obsolete
Maybe your like
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.
import java.util.*; // import all classes in this package. public class Test { public static void main(String[] args) { List nameList = new ArrayList(); nameList.add("Diego"); System.out.println(nameList); nameList.add("Grace"); System.out.println(nameList); nameList.add("Diego"); System.out.println(nameList); System.out.println(nameList.size()); } }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.
import java.util.*; // import all classes in this package. public class Test { public static void main(String[] arts) { List list1 = new ArrayList(); list1.add(new Integer(1)); System.out.println(list1); list1.add(new Integer(2)); System.out.println(list1); list1.add(1, new Integer(3)); System.out.println(list1); list1.add(1, new Integer(4)); System.out.println(list1); System.out.println(list1.size()); } }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.
import java.util.*; // import all classes in this package. public class Test { public static void main(String[] arts) { List list1 = new ArrayList(); list1.add(1); System.out.println(list1); list1.add(2); System.out.println(list1); list1.add(1, 3); System.out.println(list1); list1.add(1, 4); System.out.println(list1); System.out.println(list1.size()); } }Check your understanding
- [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.
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 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.
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);Tag » Add Element If Not In List Java
-
Add Item To Arraylist If It Does Not Already Exist In List - Stack Overflow
-
Java ArrayList Contains() - Check If Element Exists - HowToDoInJava
-
Java 8 Check Is List Contains Else Add Code Example
-
How To Check Whether Element Exists In Java ArrayList?
-
How To Find An Element In A List With Java - Baeldung
-
ArrayList (Java Platform SE 8 ) - Oracle Help Center
-
Add Element At The Beginning Of A List In Java - Techie Delight
-
How To Check If ArrayList Contains An Item In Java? - Tutorialspoint
-
Check Existence Of An Element In Java ArrayList - Tutorialspoint
-
Append() Vs Extend() Vs Insert() In Python Lists - Stack Abuse
-
How To Add Optional To List Only If Present And Exists In Java
-
What Is The ntains() Method In Java?
-
ArrayList Methods, Sorting, Traversing In Java - ZetCode
-
Java List Add() And AddAll() Methods - JournalDev
-
How To Create, Initialize & Use List In Java - Software Testing Help
-
Array.push() Element If Does Not Exist Using JavaScript | Bobbyhadz
-
Java List - Tutorials Jenkov
-
List
.Remove(T) Method (System.Collections.Generic)