Arraylist
Arraylist is a class which implements List
Interface. It is widely used because of functionality and flexibility it
offers. Most of them choose Arraylist over Arrays.
The issue with Arrays is that they are fixed in size
and so that if it full we cant add more elements over it, likewise if there are
number of elements gets removed from its memory consumption would be the same
as it doesn't shrink. On the other hand Arraylist can dynamically grow and
shrink as per need. Array List belongs to java.util package.
Creation of Arraylist
Eg :1
public class AL{
public static void main(String[]args){
//creation of Arraylist with string arguments
//adding elements to Arraylist
ArrayList<String> al=new
ArrayList<String>();
al.add("India");
al.add("US");
al.add("Australia");
al.add("Germany");
al.add("Europe");
//Displaying elements of Arraylis
System.out.println(al);
}
Output:[India,US,Australia,Germany,Europe]
Eg :2
public class AL{
public static void main(String[]args){
//creation of Arraylist with Integer arguments
//adding elements to Arraylist
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(1);
al.add(2);
al.add(3);
al.add(4);
al.add(5);
//Displaying elements of Arraylist
System.out.println(al);
}
Output:[1,2,3,4,5]
0 comments:
Post a Comment