Java Programming: Exploring Arrays in Java

array in javaAn array is a container that holds a sequence of values of similar data types. The length of the array is fixed and is defined at the time of array declaration. Each item in an array is called an element and each element of an array can be accessed using a number called array index. The first index value of array starts with zero.

Array Declaration of different data types.

  • byte [] amBytes;
  • short[] amShort;
  • boolean[] amBoolean;
  • int [] amArray;
  • char [] amChar;
  • double [] amDouble;
  • float [] amFloat;
  • String [] amString;

 Learn Java from scratch with a course at Udemy.com

An array declaration has two parts, namely the array type and array name. Array type defines the data type of contained array elements. Brackets are special symbols that describe the variable is an array.

Initializing and Accessing an Array

There are two ways to create an array using new operators and defining the values directly.

int i_am array= new int[10];

The above statement declares an integer array and the new operator allocates the size in memory of size ten.

Alternatively, we can write

int[] i_am_array={10,20,30,40,50,60,70,80,90};

In the code above, the number of values given during initialization determines the length of an array. Once the array has been declared with a given size, its size cannot be changed throughout the program.

Get to know more about Java programming with a course at Udemy.com 

Example of Defining, Storing and Retrieving Values from Array:

class ArrayDemo {

public static void main(String[] args) {

// declaration of an integer array

int[] anArray;

 // Memory allocation for 6 integers

anArray = new int[6];

 // initialize first element

anArray[0] = 10;

// initialize second element

anArray[1] = 20;

// and so forth

anArray[2] = 30;

anArray[3] = 40;

anArray[4] = 50;

anArray[5] = 60;
 
System.out.println("Element at index 0: "

+ anArray[0]);

System.out.println("Element at index 1: "

+ anArray[1]);

System.out.println("Element at index 2: "

+ anArray[2]);

System.out.println("Element at index 3: "

+ anArray[3]);

System.out.println("Element at index 4: "

+ anArray[4]);

System.out.println("Element at index 5: "

+ anArray[5]);

}
}

Output:

Element at index 0: 10

Element at index 1: 20

Element at index 2: 30

Element at index 3: 40

Element at index 4: 50

Element at index 5: 60

Generally, looping statements are used to store the values of an array, which prevents one from writing individual statements for every index separately. We can also declare a dynamic array by asking the user for input and then declaring the array.

 New to Java? Take up the beginner’s course in Udemy.com

Double Dimensional Array

These types of arrays store elements of the same data type in the form of rows and columns.

Syntax:

int [][] i_am_array=new int[4][5]

The above statement declares a double dimensional array of size 20. The first bracket denotes the row and second bracket denotes the number of columns. The index number starts from zero in the double dimensional array.

Example:

class Demo {

public static void main(String[] args)

{

// declaration of an integer double dimensional array

int[][] anArray;

// Memory allocation for 6 integers

anArray = new int[4][2];

// initialize 1st element of 1st Row and 1st Column

anArray[0][0] = 10;

// initialize 2nd element of 1st Row and 2nd Column

anArray[0][1] = 20;

// initialize 1st element of 2nd Row and 1st Column

anArray[1][0] = 30;

// So on..

anArray[1][1] = 40;

anArray[2][0] = 50;

anArray[2][1] = 60;

anArray[3][1] = 70;

anArray[3][1] = 80;

System.out.println("Element at index 1st  Row and 1st  Column: "

+ anArray[0][0]);

System.out.println("Element at index 1st  Row and 2nd  Column:"

+ anArray[0][1]);

System.out.println("Element at index 2nd Row and 1st  Column:"

+ anArray[1][0]);

System.out.println("Element at index 2ndRow and 2nd  Column: "

+ anArray[1][1]);

System.out.println("Element at index 3rdRow and 1st  Column:"

+ anArray[2][0]);

System.out.println("Element at index 3rd Row and 2nd  Column: "

+ anArray[2][1]);

System.out.println("Element at index 4th Row and 1st  Column:"

+ anArray[3][0]);

System.out.println("Element at index 4th Row and 2nd Column: "

+ anArray[3][1]);

}
}

Output:

Element at index 1st Row and 0th Column: 10

Element at index 1st Row and 1st Column: 20

Element at index 2nd Row and 0thColumn: 30

Element at index 2nd Row and 1st Column: 40

Element at index 3rd Row and 0th Column: 50

Element at index 3rd Row and 1st Column: 60

Element at index 4th Row and 1st Column: 70

Element at index 4th Row and 1st Column: 80

Parsing and Dynamic Array Creation Using a Loop

import java.util.*;

import java.lang.*;

import java.io.*;

class ArrayCopyDemo {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

// User input to take length of array

System.out.println("Enter the size of an Array \n");

int len=sc.nextInt();

// Array declaration of the size user entered

int [] a = new int[len];

//Storing array values as entered by the user.

for(int x=0;x<len;x++)

{

System.out.printf("Enter the %d element \n",x);

a[x]=sc.nextInt();

}

//Printing array values.

System.out.println(“Printing the Array values”)

for(int x=0;x<len;x++)

{

System.out.printf("The %d element is %d \n",x, +a[x]);

}

Output

Enter the size of an Array 5

Enter the 0 element 10

Enter the 1 element 20

Enter the 2 element30

Enter the 3 element40

Enter the 4 element50

Printing the Array values

The 0 element is 10

The 1 element is 20

The 2 element is 30

The 3 element is 40

The 4 element is 50

Functions Used for Array Manipulation

Copying an Array: The System class has a method named as arraycopy() that copies the data from one array to another. It takes two arrays and three integer arguments.

  • Source array
  • Destination array
  • Starting index to copy in destination array
  • Two integers that define the range of elements to be copied.

Example:

class CopyDemo

{

public static void main(String[] args)

{

// Two Character array declared

char[] copyFrom = { 'd', 'e', 'd', 'a', 'e, 'e', 'f',

'i', 'n', 'a', 't', 'e', 'd' };

char[] copyTo = new char[7];

//arraycopy() function called and two arrays are passed

System.arraycopy(copyFrom, 2, copyTo, 0, 7);

System.out.println(new String(copyTo));

}
}

Output:

daeefin

Sorting of Array

Array.sort() method in Java helps to sort the array in ascending order.

Example

int[] intArray = { 10, 2, 31, 4, 5 };

Arrays.sort(intArray);

The passed array would be sorted and saved .2,4,5,10,31

Search: We can search any element in an array by using Array.BinarySearch() function. It takes two arguments, which means that it takes one array along with one value to search in the array and returns the index of the element where the value is found.

Comparing Array

java.util.Array class contains a function called equals(). It takes two parameters in the form of two arrays and returns a Boolean value which is either true or false. If all the elements of the array matches, then it returns true. The data type compared should be similar.

Example:

int [] a = { 1, 2, 3, 4, 5 };

int [] b = { 1, 2, 3, 4, 5 };

System.out.println( Arrays.equals( a, b ) );

OUTPUT: True