Loading...
Java

One Dimensional Array

Is the basic form of array in which list of elements are stored in single variable and is accessed using only one subscript.
Syntax:
datatype arrayname[size];

Declaration of One Dimensional Array:
Array can be declared in two ways:
1. Syntax:
datatype arrayname[];
Example:
int marks[];
2. Syntax:
datatype[] arrayname;
Example:
int[] arrayname;

The size of the array should not be provided during array declaration.

Construction of One Dimensional Array:
Array in java is treated as an object so it should be created by using ‘new’ operator.
Syntax:
datatype arrayname=new datatype[size];
Example:
int marks=new int[6];

The other way of constructing an array is:
Syntax:
datatype arrayname[]; //(declaration of array)
arrayname=new datatype[size]; //(allocating memory to an array)
Example:
int marks[]
marks=new int[6];

Initialization of One Dimensional Array:
Initialization means inserting values into the array. When array is created every element is initialized with default values automatically. Loops can be used to initializing or by directly assigning the values.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *