One Dimensional Array

Card image cap

Definition of Array ?

An Array is a group of related data items that share a common name with same data type. An individual variable in the array is called an array element.

We can define an array namerollno to represent a set of rollno of group of students. An index number is present in brackets after the array name.

How to create an Array ?

To create an array, you need to perform three steps:

  • i) Declare the array
  • ii) Create memory space and
  • iii) Store data into the created memory.

Types of an Array ?

There are two types of arrays in Java they are −

  • i) One dimensional array and
  • ii) Two dimensional arrays

One Dimensional Array ?

A list of items group in a single variable name with only one index is called one Dimensional array.

Declaration of an Array ?

We can declare an array in java using subscript operator. The general forms is

datatype var_name[];
is equivalent to
datatype[] var_name[];
Here, datatype is valid java datatype and var_name is the name of the array.

Create memory space Array ?

After declaration of an array, we must allocate a memory space for the declared array. This is done with the help of new operator. The general form is:

array_name = new datatype[size];
For Example :
number = new int[10];
avg = new float[10];


Card image cap

Example 1:



Output :

 0
 0
 0
 0
 0
Card image cap

Example 2:



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

Example 3:



Output :

 1.9
 2.9
 3.4
 3.5
 Total is 11.7
 Max is 3.5
Card image cap

Two dimensional arrays :

A list of items group in a single variable name with two indexes(row and column size) is called two dimensional array.

Example 1:



Output :

 10 20 30
 40 50 60
 70 80 90
 11 21 31

Example 2:



Output :

 Length of row 1: 3
 Length of row 2: 4
 Length of row 3: 1

Example 3:



Output :

 Enter Numer of Row for Array (max 10) : 3
 Enter Number of Column for Array (max 10) : 3
 Enter 9 Array Elements : 1
 2
 3
 4
 5
 6
 7
 8
 9
 The Array is :
 1 2 3
 4 5 6 
 7 8 9
Card image cap