OCA Java SE 8 Mock Exam Review - Java Array ArrayList Review








An array stores a collection of values, a collection of primitive data types and a collection of objects.

An array itself is an object.

You can define one-dimensional and multidimensional arrays.

A one-dimensional array is an object that refers to a collection of scalar values.

A two-dimensional (or more) array is referred to as a multidimensional array.

A two-dimensional array refers to a collection of objects, in which each of the objects is a one-dimensional array.

A three-dimensional array refers to a collection of two-dimensional arrays, and so on.

Multidimensional arrays may or may not contain the same number of elements in each row or column.

The creation of an array involves three steps:

  • declaration of an array,
  • allocation of an array, and
  • initialization of array elements.

An array declaration is composed of an array type, a variable name, and one or more occurrences of [].

Square brackets can follow either the variable name or its type.

An array declaration only creates a variable that refers to null.

It's invalid to define the size of an array with its declaration.

When you allocate memory for an array, you must specify its dimensions which is the number of elements the array should store.

An array is allocated using the keyword new, followed by the type of value that it stores, and then its size.

Once allocated, all the array elements store their default values.

Elements of an array that store objects refer to null. Elements of an array that store primitive data types store 0 for integer types (byte, short, int, long), 0.0 for decimal types (float and double), false for boolean, or /u0000 for char data.

To access an element in a two-dimensional array, use two array position values.

When you combine array declaration, allocation, and initialization in a single step, the size of the array is calculated by the number of values that are assigned to the array.

You can declare and allocate an array but not to initialize it as follows

int[] a = new  int[5];

The code throws an ArrayIndexOutOfBoundsException exception if the requested index position doesn't fall in the valid range at runtime.

All the arrays can access the variable length, which stores the number or components stored by the array.

Arrays objects inherit all methods from the class Object.





ArrayList

ArrayList from the Collections framework is like a resizable array.

You may not need to set an initial size to create an ArrayList.

ArrayList implements the interface List and allows null values to be added to it.

ArrayList implements all list operations (add, modify, and delete values).

ArrayList allows duplicate values to be added to it and maintains its insertion order.

You can add a value to an ArrayList either at its end or at a specified position by using the method add.

To access the elements from ArrayList, use the enhanced for loop, Iterator, or ListIterator.

An iterator ( Iterator or ListIterator) lets you remove elements as you iterate through an ArrayList.

The method set can modify an ArrayList by either replacing an existing element in ArrayList or modifying its existing values.

remove(int) removes the element at the specified position in the list.

remove(Object o) removes the first occurrence of the specified element from the list, if it's present.

You can remove all the ArrayList elements via the method clear().

get(int index) returns the element at the specified position in the list.

size() returns the number of elements in the list.

contains(Object o) returns true if the list contains the specified element.

indexOf(Object o) returns the index of the first occurrence of the specified element in the list, or -1 if the list doesn't contain the element.

lastIndexOf(Object o) returns the index of the last occurrence of the specified element in the list, or -1 if the list doesn't contain the element.

The method toArray can return an array containing all of the elements in ArrayList in sequence from the first to the last element.