Java Array

Sharing is healthy !!

Hello everyone!! Welcome to flower brackets. Today we are going to learn java array.

what is an array?

Array is a container object and it contain elements of similar data type. Array is a data structure where we store similar elements.

We can store only fixed set of elements in a java array. Array is a collection of similar type of elements that have contiguous memory location.

It is a collection of similar data types and is a container object that holds value of homogeneous type.

Array is also known as static data structure because size of an array must be specified at the time of its declaration.

For example

Java Array

As you can see the image above, the egg tray is nothing but a container and it is equivalent to an array.

And the egg tray contains eggs and each egg is nothing but element.


Java Array declaration

Array declaration in java is as below

int[] arrayNumbers;

This is how we can declare an array of integers. Here “int” is the array’s type. “arrayNumbers” is array’s name.

And square brackets are special symbols indicating¬†“arrayNumbers” variable can hold an array.

The declaration does not actually create an array. It simply tells the compiler that this variable will hold an array of the specified type.

Below we can see declaration of array with all data types

int[] arrayNumbers;

byte[] arrayBytes;

short[] arrayShort;

long[] arrayLong;

float[] arrayFloat;

double[] arrayDouble;

boolean[] arrayBoolean;

char[] arrayChar;

String[] arrayString;

java array – store int values

Let’s see how to store integer values in java array.

Java Array

Here you can see the array which contains six elements and length of the array is six.

First index is zero and last index is five. In index two we have element 300 and at index four we have element 500.

Above array contains integer values. Array is a container object that holds a fixed number of elements of a single type.

Length of an array is established when array is created. After creation the array length is fixed.

Each entry in an array is called an element. Each element is accessed by its index.

Now, let’s see an example,

public class ArrayJavaDemo
{
public static void main(String[] args)
{
// declaring an array
int[] arrayNumber;
// allocating memory for arrayNumber
arrayNumber = new int[6];
// initializing elements
arrayNumber[0] = 100;
arrayNumber[1] = 200;
arrayNumber[2] = 300;
arrayNumber[3] = 400;
arrayNumber[4] = 500;
arrayNumber[5] = 600;

System.out.println("-----Java Array Example------"); System.out.println("Element at index zero - " + arrayNumber[0]);
System.out.println("Element at index one - " + arrayNumber[1]); System.out.println("Element at index two - " + arrayNumber[2]); System.out.println("Element at index three - " + arrayNumber[3]);
System.out.println("Element at index four - " + arrayNumber[4]);
System.out.println("Element at index five - " + arrayNumber[5]);
}
}

Output:

Java Array


length of an array

Here we are going to learn how to find the length of the array. Let’s see an example,

public class ArrayLength
{
public static void main(String[] args)
{
// declaring an array
short[] arrayNumber;
// allocating memory for arrayNumber
arrayNumber = new short[6];
System.out.println("-------------Length of an array demo----------");
System.out.println("The length of an array before initializing - " + arrayNumber.length);
// initializing elements
arrayNumber[0] = 100;
arrayNumber[1] = 200;
arrayNumber[2] = 300;
arrayNumber[3] = 400;
arrayNumber[4] = 500;
arrayNumber[5] = 600;

System.out.println("The length of an array after initializing - " + arrayNumber.length);
}
}

Output:

Java Array


array index out of bounds exception

Here I will cover when array index out of bounds exception occur and let’s understand it by an example,

public class ArrayException
{
public static void main(String[] args)
{
// declaring a byte array
byte[] arr = new byte[5];
System.out.println("The length of an array - " + arr.length); // initializing elements
arr[0] = 11;
arr[1] = 12;
arr[2] = 13;
arr[3] = 14;
arr[4] = 15;
arr[5] = 16;
arr[6] = 17;
}
}

Output:

Java Array

Above you can see the output of this program. In the above example I have declared “byte” array.

Size of the “byte” array is five and I am printing out the length of the “byte” array as you can see in the above image.

Then I have initialized seven elements. But this is not possible because size of the “byte” array is five.

But I’m trying to assign seven elements. So the array will throw¬†index out of bounds exception and the exception is thrown at fourteenth line.


how to copy array?

Here I will cover how to copy elements from source array to destination array.

Click here to learn system arraycopy method in java.


create, initialize and access an array

Ok, here we will learn how to create, initialize and access an array.

int[] arrayNumber = new int[6];

Using new operator we can create an array and above as you can see I have declared “int” array.

Size of the “int” array is six. This means it can store only six elements. To initialize elements we have to do like this,

arrayNumber[0] = 100;
arrayNumber[1] = 200;
arrayNumber[2] = 300;
arrayNumber[3] = 400;
arrayNumber[4] = 500;
arrayNumber[5] = 600;

To access or printing out array elements we have to do like this,

System.out.println("Element at index zero - " + arrayNumber[0]);
System.out.println("Element at index one - " + arrayNumber[1]); System.out.println("Element at index two - " + arrayNumber[2]); System.out.println("Element at index three - " + arrayNumber[3]);
System.out.println("Element at index four - " + arrayNumber[4]);
System.out.println("Element at index five - " + arrayNumber[5]);

Now let’s see an example for better understanding

public class ArrayCreate
{
public static void main(String[] args)
{
int[] arrNumber = new int[6];
// initializing array elements
arrNumber[0] = 11;
arrNumber[1] = 22;
arrNumber[2] = 33;
arrNumber[3] = 44;
arrNumber[4] = 55;
arrNumber[5] = 66;
System.out.println("Create, initialize and access an array example - \n");

int a = 0;
for( int number : arrNumber )
{
System.out.println("The element at index " + a + " - " + number);
++a;
}
}
}

Output:

Java Array

We can create and initialize an array in number of ways in java. Let’s see another way to create and initialize an array in java.

int[] arrNumber = {11,22,33,44,55,66};

Here length of array is determined by the number of values provided between braces.


two dimensional array

Click here to learn two dimensional array in java with an example.


multidimensional array

Here I will cover multidimensional array. Let’s see how to declare and initialize multidimensional array

Java Array

int[][] arrEven = new int[3][4];

In the above declaration you can see three and four in square brackets. This means three rows and four columns (as shown in above image).

We can access each element in multidimensional array like this,

arrEven[0][0] = 2;
arrEven[0][1] = 4;
....... so on and so forth

Multidimensional array is nothing but array of arrays. It is nothing but two or more sets of brackets.

Each element therefore must be accessed by a corresponding number of index values.

In java programming multidimensional array is an array whose components are themselves arrays.

Let’s see an example below

public class MultidimensionalArrayDemo
{
public static void main(String[] args)
{
int[][] arrEven = new int[3][4];

arrEven[0][0] = 2;
arrEven[0][1] = 4;
arrEven[0][2] = 6;
arrEven[0][3] = 8;

arrEven[1][0] = 10;
arrEven[1][1] = 12;
arrEven[1][2] = 14;
arrEven[1][3] = 16;

arrEven[2][0] = 18;
arrEven[2][1] = 20;
arrEven[2][2] = 22;
arrEven[2][3] = 24;

System.out.println("Element at arrEven[0][0] - " + arrEven[0][0]);
System.out.println("Element at arrEven[0][1] - " + arrEven[0][1]);
System.out.println("Element at arrEven[0][2] - " + arrEven[0][2]);
System.out.println("Element at arrEven[0][3] - " + arrEven[0][3]);

System.out.println("\n");

System.out.println("Element at arrEven[1][0] - " + arrEven[1][0]);
System.out.println("Element at arrEven[1][1] - " + arrEven[1][1]);
System.out.println("Element at arrEven[1][2] - " + arrEven[1][2]);
System.out.println("Element at arrEven[1][3] - " + arrEven[1][3]);

System.out.println("\n");

System.out.println("Element at arrEven[2][0] - " + arrEven[2][0]);
System.out.println("Element at arrEven[2][1] - " + arrEven[2][1]);
System.out.println("Element at arrEven[2][2] - " + arrEven[2][2]);
System.out.println("Element at arrEven[2][3] - " + arrEven[2][3]);
}
}

Output:

Java Array


multidimensional array – using loop

Let us see an example on how to execute a multidimensional array using for loop,

public class MultidimensionalArrayLoop
{
public static void main(String[] args)
{
int[][] arrEven = {{2,4,6},{8,10,12}};

int row = arrEven.length;

System.out.println("The number of rows is - " + row + "\n"); 

for(int a = 0; a < row; a++)
{
int column = arrEven[a].length;
System.out.println("The number of columns in a row - " + a + " is " + column);

for(int b = 0; b < column; b++)
{
System.out.println("The element at " + "arrEven[" + a + "] " + "[" + b + "] - " + arrEven[a][b]);
}
System.out.println();
}
}
}

Output:

Java Array


multidimensional string array

Ok come let us learn multidimensional string array.

String[][] strNames = {{ "Apple", "Fruit" }, { "Green", "Color" }, { "Audi", "Car" }};

Here I have declared a string array which is multidimensional array. We can declare a multidimensional array using two or more brackets.

As you can see declaration above I have initialized multidimensional array and has three rows and two columns.

We will see an example,

public class MultidimensionalStringArray
{
public static void main(String[] args)
{
String [][] strNames = {{ "Apple", "Fruit" }, { "Green", "Color" }, { "Audi", "Car" }};

System.out.println("The element at strNames[0][0] - " + strNames[0][0]);
System.out.println("The element at strNames[0][1] - " + strNames[0][1]);

System.out.println("\n");

System.out.println("The element at strNames[1][0] - " + strNames[1][0]);
System.out.println("The element at strNames[1][1] - " + strNames[1][1]);

System.out.println("\n");

System.out.println("The element at strNames[2][0] - " + strNames[2][0]);
System.out.println("The element at strNames[2][1] - " + strNames[2][1]);
}
}

Output:

Java Array


multidimensional string array – using loop

public class MultidimensionalStringLoop
{
public static void main(String[] args)
{
String [][] strNames = {{ "Apple", "Fruit" }, { "Green", "Color" }, { "Audi", "Car" }};
int row = strNames.length;

System.out.println("The number of rows is - " + row + "\n"); 

for(int a = 0; a < row; a++)
{
int col = strNames[a].length;
System.out.println("The number of columns in row " + a + " is - " + col);

for(int b = 0; b < col; b++)
{
System.out.println("The element at " + "strNames[" + a + "]" + "[" + b + "] - " + strNames[a][b]);
}
System.out.println();
}
}
}

Output:

Java Array


how to pass java array to method

Here we are going to learn how to pass java array to method. The advantage of passing java array to method is that we can reuse the same logic on any array.

public class JavaArrayToMethod
{
public static void main(String[] args)
{
int[] arrNumber1 = new int[5];
arrNumber1[0] = 11;
arrNumber1[1] = 22;
arrNumber1[2] = 33;
arrNumber1[3] = 44;
arrNumber1[4] = 55;
System.out.println("The maximum value in arrNumber is - "); maxValue(arrNumber1);

int[] arrNumber2 = new int[3];
arrNumber2[0] = 50;
arrNumber2[1] = 51;
arrNumber2[2] = 52;
System.out.println("The maximum value in arrNumber1 is - "); maxValue(arrNumber2);
}

private static void maxValue(int[] arrNumber)
{
int maximum = arrNumber[0];
for(int a = 0; a < arrNumber.length; a++)
{
if(maximum < arrNumber[a])
{
maximum = arrNumber[a];
}
}
System.out.println(maximum);
}
}

Output:

The maximum value in arrNumber1 is – 55
The maximum value in arrNumber2 is – 52


how to get name of the java array object?

Basically in java, array is an object and for an array object proxy class is created whose name can be obtained by getClass().getName() method on object.

public class ArrayObjectName
{
public static void main(String[] args)
{
int[] arrNumber = new int[5];
arrNumber[0] = 655;
arrNumber[1] = 535;
arrNumber[2] = 316;
arrNumber[3] = 558;
arrNumber[4] = 789;

Class<? extends int[]> obj = arrNumber.getClass();
String strName = obj.getName();
System.out.println("Name of the java array object is - " + strName);
}
}

Output:

Name of the java array object is – [I


addition of two matrices

public class AddTwoMatrices
{
public static void main(String[] args)
{
// here we are creating two matrices
int x[][] = {{2, 4, 6},{1, 3, 5}};
int y[][] = {{2, 4, 6},{1, 3, 5}};

// creating another matrix to store values
int z[][] = new int[2][3];

// now adding and printing values using for loop System.out.println("Addition of two matrices - ");
for(int a = 0; a < 2; a++)
{
for(int b = 0; b < 3; b++)
{
z[a][b] = x[a][b] + y[a][b];
System.out.print(z[a][b] + " ");
}
System.out.println();
}
}
}

Output:

Addition of two matrices –
4 8 12
2 6 10


advantages and disadvantages of array

AdvantagesDisadvantages
Array makes the code optimized. Sorting and retrieving the data is easy. We can store only fixed size of elements. Array does not grow its size at runtime.
To get data located at any index position is easier.Array structure is static. Memory allocated to an array can't be decreased or increased.
Array represents multiple data items of same type using only single nameWe must know in advance that how many elements are to be stored.
Array can be used to implement data structures like linked lists, queues, trees, graphs, stacks etc.Array elements are stored in consecutive memory locations. So insertion and deletion are difficult and time consuming.

conclusion

So this is all about java array. I hope you have understood the concept of array in java very well.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like!!!!!

Sharing is healthy !!