LinkedList and ArrayList in Java

Asked By 0 points N/A Posted on -
qa-featured

In learning Java there are many places that needs critical thinking. I am in the process of learning the Java and confused about Linked list and array list. Could anyone tell me what are these and clear comparison how these differentiate with each other. Explain with examples if possible.

SHARE
Answered By 0 points N/A #124633

LinkedList and ArrayList in Java

qa-featured

Hi,

Array and Linked List has many differences. An array is a container that holds data of one type. When you declare an array of integer, all the content of the array would be an integer. The length of an array is established once it is defined. It has a bound limit. When declaring an array, you need to define the data type first followed by [ ] and then the array name.

byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
int[] anArayOfIntegers;

And initializing array can be done this way:

int [ ] anArrayOfIntegers = new int [10]; //create an array of integers with size of 10

Accessing an array is through index, which starts from 0 to size – 1

int [0] = 12;

int [1] = 14;

In the other hand, Linked List is dynamic. The size of a Linked List grows when a new object is added and decrease if one is remove. A Linked List can be sort by stack or queue. Its has a head ( points to the beginning of the list) and a tail ( points to the end of the list) which determines where to insert new data.

Stack sort data by last in first out, means the last entry is first in the line and first entry was on the last. Queue is arranged by first in first out which was opposite to the stack. Here is an example of a Linked List implementation:

 

Login/Register to Answer

Related Questions