Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Java by (5k points)
I have a lot of confusion between the list and array. Please somebody help me out with this question.

1 Answer

0 votes
by (10.2k points)

Arrays: Java provides a very basic functionality termed as an array. Normally, users create a very simple fixed-size array in java.The array can have both objects and primitive data types of a class depending on the definition of the array. 

int arr[] = new int[8]

Arraylist: It is a collection framework in java. ArrayList can have many number of elements that can also be added later. There is no need to initialize the arrayList. But ArrayList only supports object entries, not the primitive data types. Many operations are supported by Java arrayList like indexOf(), remove(), etc.

import java.util.ArrayList; 

import java.util.Arrays; 

class Test 

public static void main(String args[]) 

/* ........... Simple Array............. */

//Specify the size for array 

int[] arr = new int[5]; 

arr[0] = 1; 

arr[1] = 2; 

arr[2] = 3; 

arr[3] = 4; 

arr[4] = 5; 

// More elements can’t be added to array arr[] 

/*............ArrayList..............*/

//Size needs to be mentioned. 

ArrayList<Integer> arrL = new ArrayList<Integer>(); 

arrL.add(1); 

arrL.add(2); 

arrL.add(3); 

arrL.add(4); 

// We can add more elements to arrL 

System.out.println(arrL); 

System.out.println(Arrays.toString(arr)); 

Browse Categories

...