1.8.2 Array
1.8.2 Array
Arrays are objects which store multiple variables of the same type. It can hold primitive types as
well as object references. In fact most of the collection types in Java which are the part
of java.util package use arrays internally in their functioning. Since Arrays are objects, they are
created during runtime .The array length is fixed.
Arrays in Java are homogeneous data structures implemented in Java as objects. Arrays store one or
more values of a specific data type and provide indexed access to store the same. A specific element
in an array is accessed by its index. Arrays offer a convenient means of grouping related information.
Example
CS8392
Arrays can be initialized when they are declared. The array will automatically be created large
enough to hold the number of elements you specify in the array initializer. There is no need to use new.
Features of Array
Array Declaration
First let us get in to declaration of array which holds primitive types. The declaration of
array states the type of the element that the array holds followed by the identifier and square braces
which indicates the identifier is array type.
int MyArray[];
Declaring Array
int []aiMyArray;
int aiMyArray[];
CS8392
Initialization:
Creating Arrays
You can create an array by using the new operator with the following syntax
Syntax
arrayRefVar = new dataType[arraySize];
The above statement does two things
● t assigns the reference of the newly created array to the variable arrayRefVar.
Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be c
Example
Following statement declares an array variable, myList, creates an array of 10 elements of double type and as
Processing Arrays
When processing array elements, we often use either for loop or foreach loop because all of
the elements in an array are of the same type and the size of the array is known.
Example
Here is a complete example showing how to create, initialize, and process arrays
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (int i = 0; i < myList.length; i++) {
System.out.println(myList[i] + " ");
}
CS8392
Example
The following code displays all the elements in the array myList –
public class TestArray
{
public static void main(String[] args)
{
double[] myList = {1.9, 2.9, 3.4, 3.5};
CS8392
}}}
Output
1.9
2.9
3.4
3.5
Example
public static void printArray(int[] array)
{
for (int i = 0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
}
You can invoke it by passing an array. For example, the following statement invokes the printArray method t
Example
printArray(new int[]{3, 1, 2, 6, 4, 2});
A method may also return an array. For example, the following method returns an array that is the reversal of
CS8392
Example
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
Example
class TwodimensionalStandard
{
public static void main(String args[])
{
int[][] a={{10,20},{30,40}};//declaration and initialization
System.out.println("Two dimensional array elements are");
System.out.println(a[0][0]);
System.out.println(a[0][1]);
System.out.println(a[1][0]);
System.out.println(a[1][1]);
}
}
Output:
10
20
CS8392
30
40