import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class obj
{
public static void main(String args[])
{
List<String> arrayList = new ArrayList<String>();
arrayList.add("kumar");
arrayList.add("raja");
arrayList.add("siva");
System.out.println(arrayList);
}
}
|
Hints
add() method is used to add Strings to the arrayList Object.
|
Output
["kumar","raja","siva"]
|
RemoveFromArrayListExample
import java.util.ArrayList;
import java.util.List;
public class RemoveFromArrayListExample {
public static void main(String[] args) {
List<String> names = new ArrayList<String>();
names.add("Kumar");
names.add("Dennis");
names.add("John");
names.add("Ravi");
names.add("Zen");
//Display names
System.out.println(names);
//Remove name using Object
names.remove("Dennis");
//Display names
System.out.println(names);
//Remove Object by index
names.remove(0);
//Display names
System.out.println(names);
}
}
|
Output
[Kumar, Dennis, John, Ravi, Zen]
[Kumar, John, Ravi, Zen]
[John, Ravi, Zen]
|
ArrayListwithContainsExample
import java.util.ArrayList;
import java.util.List;
public class ArrayListwithContainsExample {
public static void main(String[] args) {
List<String> names = new ArrayList<String>();
names.add("Kumar");
names.add("Dennis");
names.add("John");
names.add("Ravi");
names.add("Zen");
//Display names
System.out.println(names);
System.out.println(names.contains("Ravi"));
System.out.println(names.contains("Raju"));
}
}
|
Output
[Kumar, Dennis, John, Ravi, Zen]
true
false
|