An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Some data types of information can be store in to a single variable by changing subsept value.

One-Dimmensional arrays

A list of items can be given one variable name using only one subscript and such a variable is called a single-subscripted variable or a one-dimensional array.


The subscript can begin with number 0.That is x[0]. The values to the array elements can be assigned as follows:

These elements may be used in programs just like any other java variable.The following variable are valid statements.

a number = number[0]+10;
number[4]= number[0]+number[2];
number[2]= x[5] + y[10];
value[6] = number[i] *3;
The subscript of an array can be integer constants,integer variables like i,or expression that yield integers.

Creating An Array

Like any other variables,arrays must be declared must be and created in the computer memory before they are used.Creation of an array involves three steps:

1. Declare the Array
2. Greate memory locations
3. Put values into the memory lcations.

Declaration of Array

Arrays in java may be declared in two forms:
form1: Type arrayname[];
form2: Type[] arrayname;

int number[];
float average[];
int[] counter;
float[] marks;
Remember,we do not enter the sixe of the arrays in the declaration.

Two-Dimmensional Arrays

The array variables that can store a list of values.There will be situations where a table of values will have to be stored.

Two dimenssional arrays are stored in memory.As with the single dimessional arrays,each dimession of the array is indexed from zero to its maximum size -1;The first index selects the row and the second index selects the coloumwithin that row.

int my array[][];
my array = new int[3][4];

We can retriews the value stored in the second row and third column of table matrix.A quick way to intilize a two-dimensional arrays is to use neested for loops.

for(i =0; i <5; i++)
for (j=0; j<5; j++)
if (i==j)

Array Variables:

To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can reference. Here is the syntax for declaring an array variable:

The above statement does two things:

  • It creates an array using new data Type[array Size];
  • It assigns the reference of the newly created array to the variable array Refver.
  • Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement, as shown below:
  • Data Type[] array Refvar =new data Type[array Size];
  • Alternatively you can create arrays as follows:
  • Data Type[] array Refvar ={value0, value1,..., value n};
  • The array elements are accessed through the index. Array indices are 0-based; that is, they start from 0
  • to arrayRefVar.length-1.

Int [] a;
Int n,i;
System.out.println(“Enter n value”);
A=new int[n];
System.out.println(“Enter “ +n + “values”);
For (i=0;i

For (i=0;i< n;i++)
System .out.println(a[i] );

News Walls

Copyrights © 2016. All Rights Reserved to Learn Computers Home, India. //Site Designed.