http://www.technicalpage.net/search/label/SQL

>> Array VS ArrayList


Array List VS Array:

1.
Array is a set of data of same datatypes, Array List is collection of data.

2.
Array is fast , Array list is slow.

3.
The length of Array is fixed. Array List is dynamic Array , ie , the size is variable, you can add or remove elements as needed. In Array it is called length and in Array list it is size.

4.
You need to mention the Array size(length) in Array.  In Array list you can but it is not necessary.

5.
Array supports primitive data types (int, char, float) and objects. Array List supports generic datatypes such as Integer, String and objects.


codes:
6.
While declaring:

Array
int[]  intArray  =  new  int[5];
or
int[5] intArray = new int[];
or
int[]  intArray  = {11,22,33,44,55};

Array List:
Array List<Integer>    aList = new Array List<>(5); //if you want to mention the size , but it is not mandatory.
or
Array List<Integer>    aList = new Array List<>();

7.
Adding new element :

Array:
intArray[0] = 11; //Array item at index 0 is assigned the value 11.

Array List :
aList.add(5) ; // This will add the value 5 as the last element.
or
aList.add(0,1000); //This will add 1000 at the index 0.

8.
Get element/values from Array and Array List:

Array:
int x = intArray[0]; //Getting the element/value at index 0

Array List:
int x = aList.get(0); //Getting the element/value at index 0

9.
Length and Size:

Array:
intArray.length();

Array List:
aList.size();



No comments:

Post a Comment