Wednesday, 22 February 2012

Converting from ArrayList to Array & from Array to ArrayList in Java

From ArrayList to Array

Java ArrayList provides two methods to get the array back from your ArrayList.
The methods are given below
  • public Object[] toArray()
  • public Object[] toArray(Object[] a)
Let us see the difference between these two methods

1) public Object[] toArray()

Returns an array containing all of the elements in this collection. If the collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.

The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.

This method acts as bridge between array-based and collection-based APIs.



import java.util.ArrayList;
public class Main {

    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add("one");
        list.add("two");
        list.add("three");
        Object array[] = list.toArray();
        for (Object o : array) {
            System.out.println(o.toString());
        }
    }
}

The output is

one
two
three

2) public Object[] toArray(Object []a) 


The parameter a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

The array contains all of the elements in this list in the correct order.
See the example given below

import java.util.ArrayList;public class Main {

    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add("one");
        list.add("two");
        list.add("three");
        String sArray[] = new String[list.size()];
        list.toArray(sArray);
        for (String s : sArray) {
            System.out.println(s);
        }
    }
}

The output is

one
two
three

From Array to ArrayList

Java Arrays class provides a static method asList to get list from array.

public static List asList(Object[] a)

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray. The returned list is serializable and implements RandomAccess.

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

public class Main {

    public static void main(String[] args) {

        String sArray[] = { "one", "two", "three" };
        List list = Arrays.asList(sArray);
        ArrayList aList = new ArrayList(list);
        System.out.println(aList);
    }
}

The output is

[one, two, three]

Changes to the returned list affects the array and vice versa.Since the returned list is a fixed-size you can't do any operations on the list that changes the list size(i.e. add,remove).

See the examples given below 


Example1:

import java.util.List;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

        String sArray[] = { "one", "two", "three" };
        List list = Arrays.asList(sArray);
        sArray[0] = "ONE";
        System.out.println(list);
    }
}
 

The output is

[ONE, two, three]

You can see that changes made to the array after the creation of the list affects the list.

Example2:

import java.util.List;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

        String sArray[] = { "one", "two", "three" };
        List list = Arrays.asList(sArray);
        list.set(0, "ONE");
        for (String s : sArray)
            System.out.println(s);
    }
}

The output is

ONE
two
three

You can see that changes made to the returned list affects the array.

Example3:
import java.util.List;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

        String sArray[] = { "one", "two", "three" };
        List list = Arrays.asList(sArray);
        list.add("FOUR");
        for (String s : sArray)
            System.out.println(s);
    }
}

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:148)
    at java.util.AbstractList.add(AbstractList.java:108)
    at Main.main(Main.java:10)

As mentioned above, the size of the returned list is fixed.If you try to change size using any operation,the method throws UnsupportedOperationException.

No comments:

Post a Comment