Showing posts with label collection framework in java. Show all posts
Showing posts with label collection framework in java. Show all posts

Sunday, 10 May 2015

How to get sub list from ArrayList

we can use sublist(fromindex,toindex) for get sublist from a arraylist.

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

public class GetSubList {
   
    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("mysql");
        // print arraylist
        System.out.println("arrayList is = " + arr);
       
        /*
         * Returns a view of the portion of this list between the specified
         * fromIndex, inclusive, and toIndex, exclusive
         */
       
        List<String> list=arr.subList(2,4);
        System.out.println("sub list is="+list);
       
    }

}

Output:

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

sub list is=[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