Saturday, 7 March 2015

ARRAY

JAVA arrays

An array is a type of variable that can store multiple values. The array is an object in Java that contains similar data type values.
A few main points about arrays in Java:
  • Array is a data structure in java that can hold one or more values in a single variable.
  • Array in java is a collection of similar type of values.
  • Java has two types of arrays – single dimensional and multidimensional arrays.
  • Array index starts at 0.
How to declare an array :

This is how an array in java can be declared:
ArrayDataType[] ArrayName;
OR
ArrayDataType ArrayName[];
Where ArrayDataType defines the data type of array element like int, double etc.
ArrayName is the name of array.
You can also create/ Instantiate an array by using new keyword as follows:
In java, initialize an array can be done by using new keyword as well:
int arrayName = new int[10];
As you can see array initialization is done, where [10] specifies that array length is ten or array can contain ten elements.

Assigning values to arrays: 

This is how you can assign value to arrays:
arrayName[0] = 10;
Alternatively you can also assign values as follows:
int[]ArrList = {1, 2, 3, 4,5};
The above array is five elements length.

No comments:

Post a Comment