Arrays in Java

Introduction to Arrays

An array is a container object that contains the similar type of data. It can hold both primitive and object type data.
Each item in an array is called an element and each element is accessed by its numeric index.

Features of Arrays

  • Retrieve and sort the data easily.
  • Arrays are created during runtime.
  • Arrays can hold the reference variables of other objects.
  • Arrays are fixed in size.

Types of Arrays

1. Single dimensional arrays
2. Multidimensional arrays

Single Dimensional Arrays

Declaration of Single Dimensional Arrays
Give the name and type to the arrays called declaration of the array.
For example,
  • int[ ] arr or int [ ]arr or int arr[ ];
  • byte[ ] arr;
  • short[ ] arr;
  • long[ ] arr;
  • char[ ] chr;
  • String[ ] str ect.
Creating, Initializing and Accessing an Array
int arr = new int[10];    // creation of integer array of size 10

Example: An example of single dimensional array in Java.

public class ArrayDemo
{
   public static void main(String args[])
   {
      int arr[] = {2, 4, 5, 7, 9, 10};
      for(int i = 0; i < arr.length; i++)
      {
         System.out.println("arr["+i+"] = "+arr[i]);
      }
   }
}


Output:
arr[0] = 2
arr[1] = 4
arr[2] = 5
arr[3] = 7
arr[4] = 9
arr[5] = 10

Example: A java program to find the maximum and minimum number in the arrays.

public class MaxMinDemo
{
   static void max(int arr[])
   {
      int max = arr[0];
      for( int i = 1; i < arr.length; ++i)
      {
          if(arr[i] > max)
             max = arr[i];
      }
      System.out.println("Max value is: "+max);
   }
  static void min(int arr[])
  {
     int min = arr[0];
     for( int i = 1; i < arr.length; ++i)
     {
         if(arr[i] < min)
            min = arr[i];
     }
     System.out.println("Min value is: "+min);
  }
  public static void main(String args[])
   {
     int arr[] = {10, 2, 7 , 3, 16, 21, 9};
     max(arr);
     min(arr);
   }
}


Output:
Max value is: 21
Min value is: 2

The foreach loop

The foreach loop introduced in JDK 1.5 provides the traverse the complete array sequentially without using index variable.

Example: A simple program to understand the foreach loop.

public class ForEachDemo
{
   public static void main(String[] args)
   {
      int[] arr = {3, 5, 7, 11, 13, 17};
       for (int element: arr)
       {
           System.out.print(element+"  ");
       }
   }
}


Output:
3  5  7  11  13  17

Multidimensional Arrays

In multidimensional array data is stored in form of rows and columns.

Declaration of Multidimensional Arrays
  • int arr[ ][ ], or int[ ][ ] arr, or int [ ][ ]arr.
  • char arr[ ][ ] or char[ ][ ] arr or char [ ][ ]arr
  • double arr[ ][ ] or double[ ][ ] arr or double [ ][ ]arr.

Example: A sample program to create a multidimensional array.

public class MultiDimeDemo
{
   public static void main(String args[])
    {
       int arr[] [] = {{9,8,7},{6,5,4},{3,2,1}};
       for(int i = 0; i < 3; i++)
       {
           for(int j = 0; j < 3; j++)
           {
              System.out.print(arr[i][j]+" ");
           }
           System.out.println();
        }
    }
}


Output:
9 8 7
6 5 4
3 2 1

Copying Arrays

System class has a method called arraycopy(), that is used to copy data from one array to another.

Example: Simple program to copy data from one array to another.

public class ArrayCopyDemo
{  
    public static void main(String[] args)
    {  
       char[] source = { 'a', 'T', 'u', 't', 'o', 'r', 'i',  'a', 'l', 'R','i', 'd', 'e' };  
       char[] dest = new char[12];  
  
       System.arraycopy(source, 1, dest, 0, 12 );
       System.out.println(new String(dest));  
    }  
}


Output:
TutorialRide