How to convert Array to List and ArrayList to array in Java



In this tutorial we will learn how to convert Array to List  and  how to convert ArrayList to array in Java with example and programs.


How to convert  Array to List >
  • Program/Example 1.1 to convert String Array to List in java>
  • Program/Example 1.2  to convert Integer Array to List in java >
  • Also, we will create program  to convert String Array to Collection  in java >


How to convert  ArrayList to array in Java >
  • Program/Example 2.1 to convert ArrayList to String array  in java>
  • Program/Example 2.2 to convert ArrayList to Integer array in java >



How to convert  Array to List >
Program/Example 1.1 to convert String Array to List in java>

java.util.Arrays.asList method returns fixed size list, i.e. unmodifiable list

import java.util.Arrays;
import java.util.List;
public class ConvertStringArrayToListExample {
public static void main(String[] args) {
     String stringArray[] = { "a", "b" };
     //convert Array to List
     List<String> list = Arrays.asList(stringArray);
     //Display
     System.out.println(list);
}
}
/*OUTPUT
[a, b]
*/

java.util.Arrays.asList method returns fixed size list, i.e. unmodifiable list and Modifying unmodifiable list either by adding or removing elements throws UnsupportedOperationException in java. The list returned is serializable and implements RandomAccess interface.  RandomAccess is a(Marker interface) which indicate that list supports fast random access (i.e. index based access)


Program/Example 1.2  to convert Integer Array to List in java >
import java.util.Arrays;
import java.util.List;
public class ConvertIntegerArrayToListExample {
public static void main(String[] args) {
     Integer integerArray[] = { 1, 2 };
    
     // convert Array to List
     List<Integer> list = Arrays.asList(integerArray);
     // Display list
     System.out.println(list);
}
}
/*OUTPUT
[1, 2]
*/


So far in this tutorial we learned how to convert Array to List  and  now we will learn how to convert ArrayList to array in Java with example and programs.

How to convert  ArrayList to array in Java >


Program/Example 2.1 to convert ArrayList to String array  in java>
import java.util.ArrayList;
import java.util.List;
public class ConvertArrayListToStringArrayExample {
public static void main(String[] args) {
     List<String> arrayList = new ArrayList<String>();
     arrayList.add("a");
     arrayList.add("b");
    
     //Create stringArray of ArrayList's size
     String[] stringArray = new String[arrayList.size()];
     //Use toArray method to convert ArrayList to Array
     arrayList.toArray(stringArray);
    
     //Let's display the stringArray
     System.out.println("Display the stringArray");
     for(int i=0; i<stringArray.length; i++){
          System.out.print(stringArray[i]+" ");
     }
    
}
}
/*OUTPUT
Display the stringArray
a b
*/


toArray method returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

Program/Example 2.2 to convert ArrayList to Integer array in java >
import java.util.ArrayList;
import java.util.List;
public class ConvertArrayListToIntegerArrayExample {
public static void main(String[] args) {
     List<Integer> arrayList = new ArrayList<Integer>();
     arrayList.add(1);
     arrayList.add(2);
    
     //Create integerArray of ArrayList's size
     Integer[] integerArray = new Integer[arrayList.size()];
     //Use toArray method to convert ArrayList to Array
     arrayList.toArray(integerArray);
    
     //Let's display the integerArray
     System.out.println("Display the integerArray");
     for(int i=0; i<integerArray.length; i++){
          System.out.print(integerArray[i]+" ");
     }
    
}
}
/*OUTPUT
Display the integerArray
1 2
*/



Program/Example  to convert String Array to Collection  in java >
Collection is super interface of List interface, so Arrays.asList(stringArray) can be referred by reference variable of type java.util.List or java.util.Collection
import java.util.Arrays;
import java.util.Collection;
public class ConvertArrayToCollectionExample {
public static void main(String[] args) {
     String stringArray[] = {"a", "b"};
     //convert Array to Collection
     Collection<String> collection = Arrays.asList(stringArray);
     //Display Collection
     System.out.println(collection);
}
}
/*OUTPUT
[a, b]
*/

So, In this tutorial we learned how to convert Array to List  and  how to convert ArrayList to array in Java with example and programs.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>


List hierarchy in java - Detailed - ArrayList, LinkedList, vector, CopyOnWriteArrayList classes



Collection in java

Collection - List, Set and Map all properties in tabular form



No comments:

Post a Comment