Knowledge Walls
Java Master
Hyderabad, Andhra Pradesh, India
How to use Arraylist in Core Java in Examples of Core Java
1012 Views
Example
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
Previous Topics
Previous lessons of current book.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details