Skip to content

Arrays and Collection Framework

Büşra Oğuzoğlu edited this page Jul 3, 2022 · 31 revisions

Arrays and Collection Framework

1. Arrays:

Declaration:

type arrName[];
OR
type[] arrName;

Instantiation:

arrName = new type[size];

Can be combined in one statement:

type[] myArray = new type[size];

If elements are already known, declaration/instantiation can be done in the following way:

int[] nums = {1, 2, 1, 1, 3};

2. Multidimensional Arrays:

Declaration and Instantiation:

Similar to a 1D array but adding more dimensions:

int[][] intArray = new int[10][20]; //a 2D array or matrix
int[][][] intArray = new int[10][20][10]; //a 3D array

3. ArrayList:

ArrayLists are dynamic, unlike arrays. This makes them easier to use when there will be lots of manipulations to the array (adding and deleting many elements)

ArrayList<Type> arrName = new ArrayList<Type>();

4. LinkedList:

5. Stack and Queue:

6. Set (HashSet):

7. Map (HashMap):

Clone this wiki locally