http://www.technicalpage.net/search/label/SQL

>> Array List in Java

Array List in Java


Array List is one of a framework in Java collection. It is a dynamic array , ie, it's size can be varied as required during execution.  It is slower than the normal array.
Below , we have discussed different ways of using array list.

Example1:

import java.util.ArrayList;

public class TestArrayList {

       public static void main(String[] args) {
             
              ArrayList aList = new ArrayList<>();
               
              aList.add(5);
              aList.add("Earth");
              aList.add(3.14);
             
              System.out.println(aList);

       }
}

Output:
[5, Earth, 3.14]

Example 2:

Let's see array list of particular datatype :

import java.util.ArrayList;

public class TestArrayList {

       public static void main(String[] args) {
             
              ArrayList<Integer>  aList = new ArrayList<>();
               
              aList.add(5);
              aList.add("Earth"); //compilation error because of not being int
              aList.add(3.14); //compilation error because of not being int
             
              System.out.println(aList);
       }
}


Example 3:

import java.util.ArrayList;

public class TestArrayList {

       public static void main(String[] args) {
             
              ArrayList<Integer>  aListInteger = new ArrayList<>();
              ArrayList<String>  aListString = new ArrayList<>();
               
              aListInteger.add(5);
              aListInteger.add(1000);
              aListInteger.add(123);
             
              aListString.add("TX");
              aListString.add("CA");
             
              System.out.println("Integer values "+aListInteger);
              System.out.println("String values "+aListString);
             
       }
}

Output:
Integer values [5, 1000, 123]
String values [TX, CA]


Example 4:

Remove and add elements in Array List:

import java.util.ArrayList;

public class TestArrayList {

       public static void main(String[] args) {
             
              ArrayList<Integer>  aListInteger = new ArrayList<>();
              ArrayList<String>  aListString = new ArrayList<>();
               
              aListInteger.add(5);
              aListInteger.add(1000);
              aListInteger.add(123);
             
              aListString.add("TX");
              aListString.add("CA");
              aListString.add("IL");
              aListString.add("MN");
             
              System.out.println("Integer values before "+aListInteger);
              System.out.println("String values before "+aListString);
             
              aListInteger.remove(1); //remove value at index 1
              aListString.remove(2); //remove value at index 2
             
              System.out.println("Integer values after remove "+aListInteger);
              System.out.println("String values after remove "+aListString);
             
              //add Element at particular index, if no index is mentioned, it adds at the end.
              aListInteger.add(1,222); //add value at index 1
              aListString.add(2, "FL"); //add value at index 2
             
              System.out.println("Integer values after add "+aListInteger);
              System.out.println("String values after add "+aListString);
             
       }
}

Output:
Integer values before [5, 1000, 123]
String values before [TX, CA, IL, MN]
Integer values after remove [5, 123]
String values after remove [TX, CA, MN]
Integer values after add [5, 222, 123]
String values after add [TX, CA, FL, MN]

Example 5:

List Interface with Array List

import java.util.ArrayList;
import java.util.List; //new import

public class TestArrayList {

       public static void main(String[] args) {
             
              List<Integer>  aListInteger = new ArrayList<>();
              List<String>  aListString = new ArrayList<>();
               
              aListInteger.add(5);
              aListInteger.add(1000);
              aListInteger.add(123);
             
              aListString.add("TX");
              aListString.add("CA");
              aListString.add("IL");
              aListString.add("MN");
             
              System.out.println("Integer values before "+aListInteger);
              System.out.println("String values before "+aListString);
             
              aListInteger.remove(1); //remove value at index 1
              aListString.remove(2); //remove value at index 2
             
              System.out.println("Integer values after remove "+aListInteger);
              System.out.println("String values after remove "+aListString);
             
              //add Element at particular index
              aListInteger.add(1,222); //add value at index 1
              aListString.add(2, "FL"); //add value at index 2
             
              System.out.println("Integer values after add "+aListInteger);
              System.out.println("String values after add "+aListString);
             
       }
}

Output:
Integer values before [5, 1000, 123]
String values before [TX, CA, IL, MN]
Integer values after remove [5, 123]
String values after remove [TX, CA, MN]
Integer values after add [5, 222, 123]
String values after add [TX, CA, FL, MN]

Example 6:
Add existing arralylist to newly created arraylist.

import java.util.ArrayList;
import java.util.List;

public class TestArrayList {

       public static void main(String[] args) {
             
              List<String>  aOldList = new ArrayList<>();
             
              aOldList.add("Old1");
              aOldList.add("Old2");
              aOldList.add("Old3");
             
              System.out.println("Old ArrayList "+aOldList);
             
              //Let's create another arraylist and add above arraylist.
             
              List<String>  aNewList = new ArrayList<>(aOldList);
             
              aNewList.add("New1");
              aNewList.add("New2");

              System.out.println("New ArrayList "+aNewList);
             
       }
}

Output:
Old ArrayList [Old1, Old2, Old3]
New ArrayList [Old1, Old2, Old3, New1, New2]

Example 7:
Merge two Arraylists:

import java.util.ArrayList;
import java.util.List;

public class TestArrayList {

       public static void main(String[] args) {
             
              //Array List 1
              List<String>  arrayList1 = new ArrayList<>();
             
              arrayList1.add("A1");
              arrayList1.add("B1");

             
              System.out.println("ArrayList One "+arrayList1);
             
              //Array List 2            
              List<String>  arrayList2 = new ArrayList<>();
             
              arrayList2.add("A2");
              arrayList2.add("B2");

              System.out.println("ArrayList Two "+arrayList2);
             
              //Merged ArrayList
              arrayList1.addAll(arrayList2); // This returns TRUE or FALSE
              System.out.println("ArrayList Merged : "+arrayList1.addAll(arrayList2));           
             
       }
}

Output:
ArrayList One [A1, B1]
ArrayList Two [A2, B2]
ArrayList Merged : true


Example 8:
Some more functionalities :

import java.util.ArrayList;
import java.util.List;

public class TestArrayList {

       public static void main(String[] args) {
            
              List<String>  arrayList1 = new ArrayList<>();
             
              System.out.println("ArrayList is empty ? "+arrayList1.isEmpty());
             
              arrayList1.add("A1");
              arrayList1.add("A2");
              arrayList1.add("A3");
              arrayList1.add("A4");
              arrayList1.add("A5");

              System.out.println("ArrayList later is empty ? "+arrayList1.isEmpty());
              System.out.println("ArrayList "+arrayList1);
              System.out.println("Index of A3 :"+arrayList1.indexOf("A3"));
              System.out.println("Check if A4 present :"+arrayList1.contains("A4"));
              System.out.println("Array size :"+arrayList1.size());
             
              arrayList1.add(3, "A2-1"); // add element at index 3
              System.out.println("ArrayList after adding \"A2-1\": "+arrayList1);
             
              arrayList1.remove("A2-1"); // remove element
              System.out.println("ArrayList after removing  \"A2-1\": "+arrayList1);
             
              arrayList1.remove(3); // remove element at index 3
              System.out.println("ArrayList after removing  element at index3 "+arrayList1);
             
              String elementATIndex0 = arrayList1.get(0);
              String elementATIndex3 = arrayList1.get(3);
             
              System.out.println("element AT Index0 :"+elementATIndex0);
              System.out.println("element AT Index3 :"+elementATIndex3);
             
              //Last element
              String lastElement = arrayList1.get(arrayList1.size()-1);
              System.out.println("Last Element :"+lastElement);
             
       }
}

Output:
ArrayList is empty ? true
ArrayList later is empty ? false
ArrayList [A1, A2, A3, A4, A5]
Index of A3 :2
Check if A4 present :true
Array size :5
ArrayList after adding "A2-1": [A1, A2, A3, A2-1, A4, A5]
ArrayList after removing  "A2-1": [A1, A2, A3, A4, A5]
ArrayList after removing  element at index3 [A1, A2, A3, A5]
element AT Index0 :A1
element AT Index3 :A5
Last Element :A5

Example 9:
Iterator and For loop

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestArrayList {

       public static void main(String[] args) {
             
              List<String>  arrayList1 = new ArrayList<>();
             
              arrayList1.add("A1");
              arrayList1.add("A2");
              arrayList1.add("A3");
              arrayList1.add("A4");
              arrayList1.add("A5");

              //Iterator in Array List
              System.out.println("Iterator in Array List");
              int i = 0;
              Iterator<String>  elements = arrayList1.iterator();
              while ( elements.hasNext()) {
                     String element = elements.next();
                     System.out.println("element at index"+i+"="+element);
                     i++;
              }
             
              //For Loop in Array List.
              System.out.println("For Loop in Array List.");
             
              for(int x=0;x<arrayList1.size();x++) {
                     String element = arrayList1.get(x);
                     System.out.println("element at index"+x+"="+element);               
              }
       }
}

Output:
Iterator in Array List
element at index0=A1
element at index1=A2
element at index2=A3
element at index3=A4
element at index4=A5
For Loop in Array List.
element at index0=A1
element at index1=A2
element at index2=A3
element at index3=A4
element at index4=A5


Example 10:
partial and complete removeall

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestArrayList {

       public static void main(String[] args) {
             
              //Array List 1
              List<String>  arrayList1 = new ArrayList<>();
             
              arrayList1.add("A1");
              arrayList1.add("A2");
              arrayList1.add("A3");
              arrayList1.add("A4");
              arrayList1.add("A5");
             
              //Array List 2
              List<String>  arrayList2 = new ArrayList<>();
             
              arrayList2.add("A1");
              arrayList2.add("A2");
             
              System.out.println("ArrayList1 before partial removeAll : "+arrayList1);
             
              arrayList1.removeAll(arrayList2);
             
              System.out.println("ArrayList1 after partial removeAll : "+arrayList1);
             
              System.out.println("ArrayList1 is empty ? "+arrayList1.isEmpty());

              arrayList1.removeAll(arrayList1); //Complete removeAll
             
              System.out.println("ArrayList1 after complete removeAll : "+arrayList1);
              System.out.println("ArrayList1 is empty after complete removeAll ? "+arrayList1.isEmpty());
                          
       }
}

Output:
ArrayList1 before partial removeAll : [A1, A2, A3, A4, A5]
ArrayList1 after partial removeAll : [A3, A4, A5]
ArrayList1 is empty ? false
ArrayList1 after complete removeAll : []
ArrayList1 is empty after complete removeAll ? true


No comments:

Post a Comment