In: Computer Science
In your own words, explain what is meant by an Array and ArrayList. Give two examples that illustrate the main difference
Note:Java programming language
Array is a fixed length information structure though ArrayList is a variable length Collection class.We can't change length of array once made in Java yet ArrayList can be changed.
We can't store natives in ArrayList, it can just store objects.However, array can contain the two natives and items in Java.Since Java 5, natives are consequently changed over in objects which is known as auto-boxing.
Example
import java.util.*;
public class ListEx {
public static void main(String[] args) {
List<Integer> list=new ArrayList<>();
list.add(Integer.valueOf(10));//store the Integer object
list.add(20);//compiler now converts it into an Integer.valueOf(20) which is an object
list.add(30);
System.out.println("Going through the List...");
for(Integer i:list){
System.out.println(i);
}
}
}
The output will be:
Going through the List... 10 20 30