In this post i will explain how to convert an array of Strings to a list(i.e. ArrayList) by using two programs.
In first program I have converted the array to list by using List interface which is converting the array to ArrayList.
Arrays.asList(arr)
For converting Array to List we have used Arrays class which is situated in java.util.Arrays.And then we have used a asList Method which takes as argument the array and converts this array to ArrayList which is present in Arrays class i.e. java.util.Arrays.ArrayList. Here ArrayList is private static class inside Arrays.And by using this we are getting the list which is of fixed length.
So by applying any method of List we will get an Error in the program which is given below.
In first program I have converted the array to list by using List interface which is converting the array to ArrayList.
Arrays.asList(arr)
For converting Array to List we have used Arrays class which is situated in java.util.Arrays.And then we have used a asList Method which takes as argument the array and converts this array to ArrayList which is present in Arrays class i.e. java.util.Arrays.ArrayList. Here ArrayList is private static class inside Arrays.And by using this we are getting the list which is of fixed length.
So by applying any method of List we will get an Error in the program which is given below.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrToList {
public static void main(String[] args) {
String[] arr={"Hello","Hi"};
List<String> list=Arrays.asList(arr);
System.out.println("After converting Array of string to list elements are: "+list);
list.add("My");
list.add("Name is Abhishek");
list.add("Awasthi");
System.out.println("After adding the contents to list elements are: "+list);
}
}
To get rid of this type of error we can pass this ArrayList which we have got from statement
Arrays.asList(arr);
to the constructor of ArrayList which is from java.util.ArrayList as
List<String> list=new ArrayList<String>(Arrays.asList(arr));
So that all the operations of List Interface can be used.
Arrays.asList(arr);
to the constructor of ArrayList which is from java.util.ArrayList as
List<String> list=new ArrayList<String>(Arrays.asList(arr));
So that all the operations of List Interface can be used.
import java.util.*;
public class ArrayToList {
public static void main(String[] args) {
String[] arr={"Hello","Hi"};
List<String> list=new ArrayList<String>(Arrays.asList(arr));
System.out.println("After converting Array of string to list elements are: "+list);
list.add("My");
list.add("Name is Abhishek");
list.add("Awasthi");
System.out.println("After adding the contents to list elements are: "+list);
}
}
If you have any problem Please do comment.
public class ArrayToList {
public static void main(String[] args) {
String[] arr={"Hello","Hi"};
List<String> list=new ArrayList<String>(Arrays.asList(arr));
System.out.println("After converting Array of string to list elements are: "+list);
list.add("My");
list.add("Name is Abhishek");
list.add("Awasthi");
System.out.println("After adding the contents to list elements are: "+list);
}
}
If you have any problem Please do comment.
0 comments:
Post a Comment