Array in Java

An array is a container object that holds a fixed number of values of a single type in a contiguous memory location. It is a data structure which is used to store finite number of elements and all elements must be of similar data type.

Arrays are index based data structure so they allow random access to elements, they store. Indices start with '0'.

1. Array representation in memory

In this example, we have create an array of 5 elements. Indexes will range from '0' to '4'.

int[] a = new int[5];

a[0] = 1;
a[1] = 2;
a[2] = 4;
a[3] = 8;
a[4] = 16;

A pictorial representation of above example can be as below.

Array in memory
Array in memory

2. Array Features

  • Arrays are also a subtype of Object in Java.
  • Arrays are objects so we can find the length of the array using attribute 'length'.
  • Java array are types. we can declare the variables of array type.
  • Arrays are ordered and each have an index beginning from '0' for the first element.
  • Arrays can store primitives as well as objects. But all must be of a single type in one array instance.
  • Just like other variables, arrays can also be static, final or used as method arguments.
  • The size of an array must be specified by an int value.
  • Java arrays are Cloneable and Serializable.

3. Types of Array in Java

An array can be one of two types.

3.1. Single Dimensional Array

An array which store only primitives or objects is called single dimensional array. The general form of a one-dimensional array declaration is:

type var-name[];
OR
type[] var-name;

//Examples

int[] numbers;

String names[];

3.2. Multi-dimensional Array

A multi-dimensional array stores other arrays. It is array of arrays. In multi-dimensional array, each element of the array holding the reference of other array. A multidimensional array is created by appending one set of square brackets ([ ]) per dimension.

type var-name[][];
OR
type[][] var-name;

//Examples

int[][] cordinates;

String nameSets[][];

4. Array Examples

How to check if array contains an element
Java array clone example
Java array deep copy example
Convert string to string array
How to print an array
Java copy array range
How to copy an array
Convert byte array to string
Convert string to byte array
Print the content of an array
Remove duplicate elements in Array

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.