group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
262 Members
Join this group to post and comment.
Soubhik Maji
Soubhik Maji
Computer Science
14 Jan 2019

Arrays and Arraylist

Arrays

1. Collection of similar types.

2.Allocated memory at run time and of fixed size .

3.Arrays created using new are initiated with 0 for numeric types(including char), false (for boolean types ) and array of objects are initiazed with null .

Arraylist 

1.An array with dynamic length .

2.It maintains an array internally and whenever that becomes small or inefficient it copies those elements into a new bigger array .

3.Arraylist class is a generic class .You have to specify what kind of arraylist we want , whether string or integer .

4.When you initially create a arraylist its size is zero 

ArrayList friends=new ArrayList<>(); 

5.Now when you do 

friends.add("peter");

friends.add("Paul");

Its length gets increased to 2 .

6.You can add or remove elements anywhere in the arraylist .

friends.remove(0);//removes based on syntax

friends.add(0,"paul");//adding based on index ..adds before index zero 

friends.set(1,"harry");//replaces an element 

friends.get(0);//retrieves an element  based on index 

friends.size();//returns size/length of arraylist 

7.You cannot have an ArrayList of primitive types .

8.Automatic conversion from primitive types to its object type is called autoboxing . This happens when you add int to a ArrayList of Integer .

9. Be careful while comparing elements if Arraylist using == and != they compare the object reference and not contents of the object . You should first unbox them using automatic inbox by holding them into their primitive types and them comparing them 

Shruthi Pillai
Shruthi Pillai
Information Technology
3mos ago

ARRAYS

It is a dynamically created object that hold values of same type.The variables in the array are ordered and each have an index beginning from 0 . Arrays are useful when executing calculations within a loop. The size of an array must be specified by an int value. The direct super class of array type is Object.

Valid declaration syntax are

EG: int[] boxes   OR

       int boxes[]

To access the first element we use boxes[0];

ARRAYLIST

Arraylist is a resizable array, can be found in java.util package. It is helpful in programs where lots of manipulation in the array is needed . Java ArrayList allows us to randomly access the list.

Syntax example:

ArrayList cars = new ArrayList();

Soubhik Maji
Soubhik Maji
Computer Science
3mos ago

Can you give me summary of java collections framework ?