Array in Java

Array in Java


Array in Java


What is an Array?

The contiguous memory location is called Array. Actually, an array is a plural, so that we can simply read the variable and recognize that it is a collection of data. It is an object in Java that holds a fixed number of values of a single data type. Java Array is a data structure in java that can hold one or more values in a single variable. An array is declaring like a variable except that we are attaching the square brackets [], which can be used in two parts, either before the name of the variable or after the name of the variable.

Why do we need an array?

Suppose you want to store the name of 500 employees, so we need to define 500 variables but this could be a very big problem so we need a better solution for defining 500 names for employees, that is why need an array.


Java array index starts at 0.
Array

Write down some points on Array.

  • The array is a homogeneous data structure that stores elements of a similar type.
  • All the elements of an array share the same name.
  • Each element of an array treated separately and store in a different memory location.
  • Each value stored, in an array, is known as an element and all elements are indexed. The first element added, by default, gets 0 indexes. That is, the 6th element added gets an index number of 5.
  • Java checks the boundary array. If the index goes beyond, an exception will generate.
  • Elements of an array can be retrieved by their index number.
  • Java arrays are objects. With methods, the array elements can be manipulated.
  • Memory allocation in Java arrays are dynamically by using the new operator.
  • Arrays can be multidimensional.
  • Once the size of an array defined, it cannot grow it or shrink it at run time. 


Declaring an Array

Int number [];
The above statement just declares an array object, but it doesn’t have any elements. An array is a null object. It must be instantiated using the following statement:
Number=new int [10];
In Java, every object is instantiated using the ‘new’ keyword.

Initialization of one dimensional array

Int arr[] = {5, 6, 1, 9, 3};
Array arr[] contains five elements in it.

Types of Array in java

There are two types of array.

  1. Single Dimensional Array: Int arr[]=new int[10];
  2. Multidimensional Array: Int arr[][]=new int[5][5];

Java Array Literals

Sometimes we need predefine values for the array, so Java gives us a better solution for that, so we can use an array literal. Here is how we cab defines an array literal:
int[] number = {5, 4, 8, 9, 12, 6};
or
int number[] = {1,2,3,4,5,7,8};
Here we don’t need a new operator for defining the array.
So here values which are store inside the curly brackets is called an array literal.
For defining String array we can do so as follows
String name[] = {“Alam”, “Binny”, Rakesh”, “Salim”, “Sachin”};

Some examples:

// Simple Array print
import java.io.*;
public class SimpleArrayPrint
{
    public static void main(String args[])throws IOException
    {
            InputStreamReader ab=new InputStreamReader (System.in);
            BufferedReader br=new BufferedReader (ab);
            int n[]=new int[10];
            int i;
            System.out.println ("Enter Five numbers:");
            for (i=0;i<5;i++)
            {
                                                                n[i]=Integer.parseInt(br.readLine());
                                                }
                                                System.out.println ("Out Put");
                                                for (i=0;i<5;i++)
                                                {
                                                                System.out.print (n[i]+" ");
                                                }
                }
}

Example
// Array Summation print
import java.io.*;
public class SumArrayPrint
{
    public static void main(String args[])throws IOException
    {
            InputStreamReader ab=new InputStreamReader(System.in);
            BufferedReader br=new BufferedReader(ab);
            int n[]=new int[10];
            int i,s=0;
            System.out.println("Enter ten numbers:");
            for(i=0;i<10;i++)
            {
                                                                n[i]=Integer.parseInt(br.readLine());
                                                }
                                                System.out.println("Out Put");
                                                for(i=0;i<10;i++)
                                                {
                                                                s=s+n[i];
                                                }
                                                System.out.println("Sum="+s);
                }
}


Advanced Java : Learn Java Advanced Features


Output
Enter ten numbers:
2
3
65
66
5
8
12
4
54
5
Out Put
Sum=224

Example

// Array program of Odd and Even

import java.io.*;
public class ArrayOddEven
{
    public static void main(String args[])throws IOException
    {
            InputStreamReader ab=new InputStreamReader(System.in);
            BufferedReader br=new BufferedReader(ab);
            int a[]=new int[10];
            int ev[]=new int[10];
            int od[]=new int[10];
            int i,j,k;
            j=k=0;
            System.out.println("Enter Five elements:");
            for(i=0;i<10;i++)
            {
                a[i]=Integer.parseInt(br.readLine());
            }
            for(i=0;i<10;i++)
            {
                if(a[i]%2==0)
                {
                  ev[j]=a[i];
                  j++;
                }
                else
                {
                    od[k]=a[i];
                    k++;
                }
            }
            System.out.println("Even Array");
            for(i=0;i<j;i++)
            {
                System.out.println(ev[i]);
            }
            System.out.println("Odd Array");
            for(i=0;i<k;i++)
            {
                System.out.println(od[i]);
            }
    }
}
Output
Enter Five elements:
4
5
6
3
2
1
7
8
9
2
Even Array
4 6 2 8 2
Odd Array
5 3 1 7 9


// Double Dimention Array Example

import java.io.*;
class DBLArray
{
public static void main(String arg[])throws IOException
{
int n[][]=new int[3][3];
int i,j;
InputStreamReader ab=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ab);
System.out.println("Enter Data :");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
n[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("OutPut Data");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(n[i][j]+" ");
}
System.out.println("");
}
}
}

Java Array Exception example

public class ArrayException
{
    public static void main(String args[])
    {
        int n[]={5,6,7,8,9};
        int i,m;
        try
        {
            m=n[4];
            System.out.println(m);
        }
        catch(Exception e)
        {
            System.out.println("O no not again");
        }
        finally
        {
            System.out.println("Must execute");
        }
        System.out.println("Thank you");
    }
}

Some more program

Binary Search in Java


Learn Java
Become An Awesome Java Professional

SHARE THIS
Previous Post
Next Post