Showing posts with label arraylist example. Show all posts
Showing posts with label arraylist example. Show all posts

Sunday, 10 May 2015

How to use retailAll in arraylist

Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection.


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

public class RetainAll {
   
    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<String>();
       
        // add element in arraylist
       
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
        arr.add("delhi");
        arr.add("INDIA");
       
        // print arraylist
       
        System.out.println("arrayList is = " + arr);
   
        List<String> brr = new ArrayList<String>();
       
        brr.add("java");
        brr.add("shivam");
       
        /*
         *  removes from arr all of its elements that are
         *  not contained  in the brr.
         */
       
        arr.retainAll(brr);
       
        System.out.println("After retainAll arr is="+arr);
       
    }

}

 Output:

arrayList is = [c, php, html, java, delhi, INDIA]

After retainAll arr is=[java]


Related Posts: 

 


Storing Java Object In ArrayList






How to count number of element in ArrayList

Size() Method return number of element in the arraylist.

import java.util.ArrayList;

public class CountElement {

    public static void main(String[] args) {

        ArrayList<String> arr = new ArrayList<String>();

        // add element in arraylist

        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
        arr.add("delhi");
        arr.add("INDIA");

        // print arraylist

        System.out.println("arrayList is = " + arr);

        /*
         * Returns the number of elements in this list.
         */

        System.out.println(" size = "+arr.size());

    }

}

Output:

arrayList is = [c, php, html, java, delhi, INDIA]

 size = 6


Related Posts: 

 


Storing Java Object In ArrayList






How to shuffle elements in ArrayList

import java.util.ArrayList;
import java.util.Collections;

public class Shuffle {

    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<String>();
        // add element in arraylist
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
        // print arraylist
        System.out.println("arrayList is = " + arr);
       
        /*
         * Randomly permutes the specified list using a default
           source of randomness
         */
       
        Collections.shuffle(arr);
       
        for (String el : arr) {
            System.out.println(el);
        }
       
        Collections.shuffle(arr);
       
        System.out.println("Second Time shuffle");
        for (String el : arr) {
            System.out.println(el);
        }
       
    }
}

Output:

arrayList is = [c, php, html, java]

java
c
php
html

Second Time shuffle

php
java
html
c


Related Posts: 

 


Storing Java Object In ArrayList





How to find does ArrayList contains elements or not

we use contains() and  Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ?  e==null : o.equals(e)).

import java.util.ArrayList;

public class ContailsElement {

    public static void main(String[] args) {
       
        ArrayList<String> arr = new ArrayList<String>();
        // add element in arraylist
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
        // print arraylist
        System.out.println("arrayList is = " + arr);
        /*
         * Returns true if this list contains the specified element
         */
        System.out.println(arr.contains("php"));
       
        System.out.println(arr.contains("more"));
    }
   
}


Output:

arrayList is = [c, php, html, java]

true

false






Related Posts: 

 


Storing Java Object In ArrayList










How to get element of Arraylist

 We  use method  get(int index) for get any element at specific index.
 
import java.util.ArrayList;

public class GetElement {
   

    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<String>();
        // add element in arraylist
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
        // print arraylist
        System.out.println("arrayList is = " + arr);

        System.out.println("Element At Index 2 ="+arr.get(2));
    }
   
}

Output:

arrayList is = [c, php, html, java]

Element At Index 2 =html



Related Posts: 

 


Storing Java Object In ArrayList




How to Remove List From Arraylist

 We can remove  all element of list from arraylist using removeAll().

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

public class RemoveList {

    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<String>();
        // add element in arraylist
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
        // print arraylist
        System.out.println(" berfore remove arrayList is = " + arr);
      
        List<String> brr = new ArrayList<String>();
        brr.add("php");
        brr.add("java");
      
        /*
         * Removes from this list all of its elements that are
           contained in the specified collection.
         */
      
        arr.removeAll(brr);
      
        System.out.println("after remove arraylist is = "+arr);
      
    }
  
}

 Output:

 berfore remove arrayList is = [c, php, html, java]

 after remove arraylist is = [c, html]


Related Posts: 

 


Storing Java Object In ArrayList








how To Remove Element of arraylist

There we will remove one element of arraylist using remove().


import java.util.ArrayList;

public class RemoveElement {
              
    public static void main(String[] args) {
      
        ArrayList<String> arr = new ArrayList<String>();
      
        // add element in arraylist
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
      
        // print arraylist
        System.out.println("Before Remove Element ArrayList is = " + arr);
      
        /*
         * Removes the element at the specified position in this
           list. Shifts any subsequent elements to the left
         */
      
        arr.remove(1);
      
        System.out.println("After Remove Element ArrayList is="+arr);
      
    }
}

 Output:

Before Remove Element ArrayList is = [c, php, html, java]

After Remove Element ArrayList is=[c, html, java]




Related Posts: 

 


Storing Java Object In ArrayList




Remove All Element From Arraylist Example

 Remove All Element From Arraylist Example

Method 1:

We can remove all element of arraylist using clear() method . clear() removes all element from the list and return empty list.

import java.util.ArrayList;

public class RemoveAll {
   
    public static void main(String[] args) {
       
        ArrayList<String> arr = new ArrayList<String>();
       
        // add element in arraylist
        arr.add("c");
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
       
        // print arraylist
        System.out.println("arrayList is = " + arr);
       
        /*
         * Removes all of the elements from this list.
         */
        arr.clear();
       
        System.out.println("After Clear arrayList is="+arr);
    }
   
}

Output:

arrayList is = [c, c, php, html, java]

After Clear arrayList is=[]

Method 2:


import java.util.ArrayList;

public class RemoveAll2 {

    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<String>();
        // add element in arraylist
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
        // print arraylist
        System.out.println("Before Removing arrayList is = " + arr);
       
        arr.removeAll(arr);
        System.out.println("After Remove all element of aarraylist is="+arr);
       
    }
   
}


Output:

Before Removing arrayList is = [c, php, html, java]

After Remove all element of aarraylist is=[]



Related Posts: 

 


Storing Java Object In ArrayList




Swap elements of Java ArrayList example

We Swap List element using swap method of    Collections.

import java.util.ArrayList;
import java.util.Collections;

public class SwapElement {


    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<String>();
        // add element in arraylist
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
        // print arraylist
        System.out.println("ArrayList Before Swap is = " + arr);

        /*
         * Swaps the elements at the specified positions in the specified list.
         *  (If the specified positions are equal,
           invoking this method leaves the list unchanged.)
         
          Parameters:
       
        list The list in which to swap elements.
        i the index of one element to be swapped.
        j the index of the other element to be swapped.
      
        Throws:
      
       IndexOutOfBoundsException - if either i or j is out of range (i < 0 || i >= list.size() || j < 0 || j >=
        list.size()).
         */

        Collections.swap(arr, 1, 3);
       
        System.out.println("ArrayList Before Swap is = " + arr);
       
    }
   
   
}


Output:

ArrayList Before Swap is = [c, php, html, java]

ArrayList Before Swap is = [c, java, html, php]



Related Posts: 

 


Storing Java Object In ArrayList



How to add all elements of a list to ArrayList

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

public class AddAllElement {

    public static void main(String[] args) {
       
        ArrayList<String> arr = new ArrayList<String>();
       
        // add element in arraylist
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
       
        // print arraylist
        System.out.println("arrayList is = " + arr);
       
        List<String> brr = new ArrayList<String>();
        brr.add("one");
        brr.add("second");
       
        //Appends all of the elements in the specified collection
        //to the end of this list, in the order that they are
        //returned by the specified collection's Iterator.
        arr.addAll(brr);
       
        System.out.println("After AddAll list brr ="+arr);
       
        List<String> linkList = new LinkedList<String>();
        linkList.add("Delhi");
        linkList.add("Kurukshetra");
   
        arr.addAll(linkList);
      
        System.out.println("After AddAll list linklist"+arr);
    }
   
}

Output:

arrayList is = [c, php, html, java]

After AddAll list brr =[c, php, html, java, one, second]

After AddAll list linklist[c, php, html, java, one, second, Delhi, Kurukshetra]



Related Posts: 

 


Storing Java Object In ArrayList







Saturday, 9 May 2015

Reverse order of all elements of ArrayList

Reverse order of all elements of ArrayList


import java.util.ArrayList;
import java.util.Collections;

public class ReverseAllelement {
   
    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<String>();
        // add element in arraylist
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
        // print arraylist
        System.out.println("Before Reverse ArrayList Is = " + arr);
       
        Collections.reverse(arr);
       
        System.out.println("After Reverse ArrayList Is = " + arr);
    }

}

Output:

Before Reverse ArrayList Is = [c, php, html, java]

After Reverse ArrayList Is = [java, html, php, c]




Related Posts: 

 


Storing Java Object In ArrayList



How To Copy ArrayList To Array

 How To Copy ArrayList To Array

 In this example we learn how to make a array from arraylist. we will use toArray() method for this purpose. This method return a array from arraylist 

import java.util.ArrayList;

public class ArrayListToArray {

    public static void main(String[] args) {
       
        ArrayList<String> arr = new ArrayList<String>();
        // add element in arraylist
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
   
        String[] brr = new  String[arr.size()];
        //Returns an array containing all of the elements in this
         //list in proper sequence (from first to last element).
        arr.toArray(brr);
        for (int i = 0; i < brr.length; i++) {
              System.out.println(brr[i]);       
        }
    }
}

Output:

c
php
html
java

Related Posts: 

 


Storing Java Object In ArrayList




Thursday, 7 May 2015

How To Create ArrayList In Java


How To Create ArrayList In Java

In this program we make a arraylist and add element in it.Here

ArrayList<String>  arr = new ArrayList<String>();

String tell that arraylist arr store string type elements.


import java.util.ArrayList;

public class CreateArrayList {

    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<String>();
        // add element in arraylist
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
        // print arraylist
        System.out.println("arrayList is = " + arr);
    }

}

Output:

 arrayList is = [c, php, html, java]