Java Array Tutorial

Java Array is a data structure type which is used to store multiple variables of the same type. Arrays can hold Primitives as well as Object. The variables in the array are ordered but not sorted. Each element in the array have an index and starting index for element in array is 0.

An array will always be an Object in Heap. No matter what it store, primitives or Object.

Array Declaration (Syntax)

Array of Primitives

		//Single Dimensional Array
		int[] arr;  //recommended
		int arr[];

		//Multi Dimensional Array
		int[][] arr;  //recommended
		int arr[][];
		int[] arr[];Array Of Objects

Array Of Object

		//Single Dimensional Array
		String[] arr; //recommened
		String arr[];

		//Multi Dimensional Array
		String[][] arr; //recommened
		String arr[][];
		String[] arr[];
 Note: You cannot include the size of an array in the declaration.

Declaration only doesn’t create array object in heap.

Constructing a Java Array

This is the step where array object will be created on the heap. As once created the size of the array can not be changed, hence the size of the array needs to be provided at the time of constructing it. This size will be used by JVM while creating an array object on Heap.

Size of the array means how much element an array can contain.

One Dimensional Array

New keyword will be used to construct one/multidimensional array.

		int[] arr; //declares a new array
		arr = new int[10]; One Dimensional Array

Two Dimensional Array

These are the array of arrays. So a two-dimensional array is an array of arrays of int. So when you say, you are creating a two-dimensional array of Long primitive it means it will be an array containing multiple elements which in itself an array of Long.java Multidimensional Array

		int[][] arr;
		arr = new int[10][];

Only the first part(First Dimension is required) needs the size and not the all.

Initialising Array

Once arrays created and space assigned to it next thing would be to add elements in it. Initialisation of array is the place where we can do this(Adding Element in an array).

Single Dimensional Array

		int[] arr  = new int[10];
		arr[0] = 0;
		arr[0] = 1;
		int[][] arr  = new int[10][]; //   Multi Dimensional Array   
		arr[0][0] = 0;
		arr[0][1] = 1;

Java Array Literals

In java if you want to create arrays of primitives types or string and the values are fixed then you can utilise the array literal’s syntax. Here is how you can define array literals.

String[] strArray = new String[]{ “J”,”B”,”T” };

String[] strArray = { “J”,”B”,”T” };

{} are used to define the values that needs to be inserted in array. And values inside {} will determine the size of the array.

Accessing Array Elements

Each element in the array has an index. Indexing starts from 0, meaning first element in array will have 0th index and second element will have index as 1. These index numbers can be used to access specific element in array.  Same index can be used to set the value in array at given index.

String[] strArray = { “J”,”B”,”T” }
strArray[0] = “CHANGED”;
String valueAtIndex = strArray[0];

Last element in an array will have an index number as (size_of_the_array – 1).

Java Array Cheat-sheet

  • Arrays are collection of variables of the same type.
  • Arrays are always created in HEAP.
  • Arrays can contain both Primitives and Objects.
  • Size of Array cannot be included in Declaration.
  • The declaration doesn’t create array object in heap.
  • Once an array has been created its size cannot be changed.
  • Length of an array can be accessed vie it’s length field.
  • Java for loop or enhanced loop can be used to iterate array elements.

Array Example

public class JavaArrayBasic {

	public static void main(String args[]) {

		/*
		 * Array needs to be initialized before it can be used
		 */
		int intArr[] = { 1, 2 };
		int intArr1[];

		// intArr1 can not be used @ this place as it has not been initialized yet

		/*
		 * intArr = {1,2}; Array constants can only be used in initializers, So
		 * above line will give compile time error
		 */

		for (int i = 0; i < 2; i++)
			System.out.println("Value in array @ index" + i + " is "
					+ intArr[i]);

	}
}

 

17 Comments Java Array Tutorial

  1. nisha

    Awesome, very well written article. Many important points are covered here. your article is very petrified me in the learning process and provide
    additional knowledge to me , maybe I can learn more from you, thank you

    Reply
  2. Anurag Singh

    Hello sir,

    this java array post is rally helpful for everyone who want to learn java array easily.
    you explained very well.
    please keep posting.
    thank you.

    Reply
  3. Debajyoti

    how to find a unique row from a 2D array where the row is not equals to other row or not reverse of any other row

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.