Java | Arrays

05 min read

Arrays:

  • Collection or group of similar data items referred to by a common mane.
  • In Java, arrays are implemented as objects.
  • Arrays are immutable (ie., its size cannot be changed once given while declaring it).
  • As arrays are implemented as objects, they are also created using 'new' operator, so that they are created during run time (dynamically).

Syntax:

type[ ] ref variable;

ref variable = new type[size];

(or)

type[ ] ref variable = new type[size];

Example:

Suppose an integer array has to be created with 10 elements in it, the following statement has to be provided.

  1. int[ ] a = new int[ 10];
  2. int[ ] a - Creates a reference variable of type 'integer array', so as to store the address(reference) returned by 'new' operator.
  3. new int[ 10]; -This allocates spaces for holding 10 integers and returns the starting address(reference)of that memory block.

Remember: The reference variable which stores the starting address of this array should also be of same array type.

Though array is an object in Java, there are a few differences between array object and other objects.

  • Though all the array elements are basically instance variables in the array object, they are referred to by number, rather than by name.
  • No array classes have to be defined explicitly to create array objects.

The array object also contains 'length' instance variable in addition to the elements of the array. Thus, if an array object of size 10 is created, there will be 11 instance variables in total, including 'length'. 'length' gives the total number of elements that the array is designed to hold and not the number of elements that are actually in use.

For the previous example, the array declaration statement can also be written as follows (as in C)

int a[ ] = new int[ 10];

However, the former method gives a clear interpretation of the array object creation.

Note: Thus, in 'public static void main(String[] args)' statement also, 'String[] args' bears better interpretation than 'String args[]'. However, the latter is also valid.

All array elements are auto-initialized to zero for numeric t es false for boolean and null for reference . Every string in Java is an object; thus the elements of String array are references and hence initialized to 'null'.

Note: Array elements are zero, false or null depending on the type, by default. But the default value for all types of reference variables is

  1. null, if it is a class variable.
  2. undefined, if it is a local variable in a method.

 

Array initialization: 

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

(or)

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

When the two statements are executed the arrays will be automatically created large enough to hold the number of elements specified in the array initializer.

String[ ] s = {"Anu", "Ajay", "Akhil"};

 

Copying Arrays:

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

int[ ] b;

b=a;

This copies the array reference variable 'a' into 'b' ie., both the reference variables have the same address which is the startint! address of the array. Hence both point to the same array, resulting in only 1 array, being referred to by 2 variables. If you actually want to copy all the values of one array to another, you have to use the arraycopy( ) method, defined in the System class.

System.armycopy(fromarray, fromindex, toarray, toindex, count); 

where,

fromarray — indicates the source array

fromindex — indicates the index in source array from where copying is to be done

toarray — indicates the destination array. It must have sufficient space to hold the copied elements

toindex — indicates the index in destination array from where copying is to be done

count — indicates the number of elements to be copied.

Example:

int[ ] a = new int[10];
int[ ] b = new int[5];

for(int i=0;i< 1 0;i++)
a[i] = 1+1;
System.arraycopy(a, 0, b, 2, 2); -› (This copies Oth& 1st elements of a[ ] array into the 2nd& 3rd locations of b[ ] array since start index  of a[ ] is 0 and start index of b[ ] is 2 and number of elements to copy is 2.)
for(int i=0;i<5;i++)                       
System.out.println(b[i]);         

Output:
0 0 1 2 0

(1 2) - except these 2 values, all other elements retain their original default values in the output.

 

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error