Easy Tutorial
For Competitive Exams

Java Programming Arrays

Why use array?

To store the marks of all the students of a university,you need to declare thousands of variables.

In addition each variable name needs to be unique,as per the variable naming rules.

It is practically impossible for a person to remember the name of each variable.

To avoid such situations,we can use array.

What is array?

Array is a collection of homogeneous data elements. It stores the value on the basis of the index value.

It is fixed in size means that you can't increase the size of array at run time.

Index of array starts from zero to size-1.

Every array in a Java is an object, Hence we can create array by using new keyword.

By using loop we can access all the elements of array.

Types of Array

Single Dimensional Array

Multidimensional Array

How to declare 1D array?

Syntax

Datatype arrayname[];
int numbers[];

How to allocate memory to a 1D array:

Syntax

datatype arrayname=new datatype[size];
int numbers[]=new int[10];

How to initialize 1D array?

Syntax

Datatype arrayname[]={value1,value2…..valuen};
int  numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

How to print values of 1D array?

Print values of array

public class Demo{
public static void main(String args[]){
int a[] = {10,20,30,40,50};
for (int i=0; i<5; i++){
System.out.println(a[i]);
       }
   }     
}
Try it Yourself

Exercise Q&A

10797.Which of these is an incorrect array declaration?
int arr[] = new int[5];
int [] arr = new int[5];
int arr[];
arr = new int[5];
int arr[] = int [5] new;
10798.What is the output of this program?
public class array_output {
    public static void main(String args[]) {
        int array_variable [] = new int[10];
	 for (int i = 0; i < 10; ++i) {
             array_variable[i] = i;
          System.out.print(array_variable[i] + " ");
           }
     } 
}
0 1 2 3 4 5 6 7 8 9
1 2 3 4
1 2 3 4 5 6
1 2 3 4 5 6 7

How to declare 2D array?

Syntax

Datatype arrayname[][];
int numbers[][];

How to allocate memory to a 2D array:

Syntax

Arrayname[][]=new datatype[row][column];
int numbers [][]=new int[5][5];

How to initialize 2D array?

Syntax

Datatype arrayname[][]={
{row1…..row1n},
{row2….row2n},
};
int m[][] = {{ 1, 2, 3, 4 },
             { 5, 6, 7, 8 },
             { 9, 10, 11, 12 },
             { 13, 14, 15, 16 }};

How to print values of 2D array?

Print values of 2D array

public class Demo {
    public static void main(String args[]) {
       int i, j;
       int m[][] = {{ 1, 2, 3, 4 },
                    { 5, 6, 7, 8 },
                    { 9, 10, 11, 12 },
                    { 13, 14, 15, 16 }};
        for(i=0; i<4; i++){
        for(j=0; j<4; j++){
          System.out.print(m[i][j] + " ");
          }
       System.out.println();
     }
   }
}
Try it Yourself

Solve this!!

10799.Examine the following:what is in values[2][1] ?
double[][] values =
    { {1.2, 9.0, 3.2},
    {9.2, 0.5, 1.5, -1.2},
    {7.3, 7.9, 4.8} } ;
7.3
7.9
4.8
9.2
10800.What is the output of this program?
public class array_output {
   public static void main(String args[]) {
             int array_variable[][] = {{ 1, 2, 3}, 
                                      { 4 , 5, 6}, 
                                      { 7, 8, 9}};
             int sum = 0;
             for (int i = 0; i < 3; ++i)
             for (int j = 0; j <  3 ; ++j)
                 sum = sum + array_variable[i][j];
                 System.out.print(sum / 5);
        } 
}
5
6
7
9
Share with Friends