Array in Java with Examples | Tutorials- Scratcharound

No comments

What is an Array in JAVA ?

In java Array is an object which can store multiple variables of same type. It can either store references of objects of same type or primitives of same type. But always remember an Array in Java is always is an Object in the Heap. Arrays have a fixed size, ie. we cannot change its size once its been declared.
Note:As stated above an array only holds the references of Objects, but actual Objects are created on the Heap.

How to declare an Array in Java

Write the type of element array will hold, followed by the square brackets and then the identifier either on the left or right side of the square brackets.

int [ ] arr - Array declaration to hold the int values.
Apple [ ] apples - Array declaration to hold the Apple object references.
Note:Declaring an array with any number written insde the square brackets like 'int [5] arr' is always illegal, while declaring an array in java.
In above section we have declared an array, but we have not constructed any real Array object n the heap. Now We will see how to construct an array that will have a real existence in the heap memory.

Constructing An Array In Java

Constructing an one dimensional empty array in java:
To construct an array in java we can use new keyword with as given below -
int [ ] arr = new int [5];
arr = new int [5];
OR
int [ ] arr = new int [5]
For objects -
Apple [ ] apples = new Apple[5];
While constructing an array we also define the number of elements it can have, which defines the memory allocated for that array in the heap memory. We write the number of elements array can have inside the square brackets as shown in the above syntax.
Note: Note that in above statement we have declared and constructed the array in a single statement, which is perfectly legal.
Notice that in the above apples array we constructed, no object of Apple has been created but only the empty references inside the Apples array. We can use these references to point to some Object of type Apple on the heap.
Constructing and initializing an one dimensional array in java:
We can also construct an array an initialize it simultaneously -
int [ ] arr = {1,3,40,3}
Above statement will create an array of integers have the elements specified inside the curly braces and with size 4(no of elements inside the curly braces).

Creating Multidimensional Array In Java

In java a multidimensional array is an array of arrays, ie. each element of the array will hold the reference to some another array. For example a 2 dimensional array, will have elements with each element holding the reference to some other array on the heap, an that another array will hold the actual values.
Example :
int arr [  ][  ] = new int [3][  ]
arr[0] = new int[2];
arr[0][0] = 3;
arr[0][1] = 9

arr[1] = new int[1]
arr[1][0] = 8;

Explained: Above 2-D array arr is of size 3 and has referene to another arrays on each of its index 0 and 1, while index 3 has nothing yet.
Index of an array always starts with zero, so if we have a array of length 8 then the index will start at 0 and will end at 7.

What we can put inside an array of some specific type(int [ ], String [ ], Apple [ ] and others)

Array of primitives:
An primitive type array can accept any type of value that can be accepted by the declared type,for example -
We can put a byte ,char or short inside an primitive array of type int, because these values can be accepted by an integer type.
Example:
int [  ]arr = new int[4];
short s = 2;
int i = 12;
byte b = 1;

arr[0] = s;
arr[1] = i;
arr[2] = b;

Array of Object References:
In an array of Object references can accept object of any subclass of the declared class;
Example:
Fruit [  ] fruits = new Fruit[4];
Apple extends Fruit {};
Mango extends Fruit {};

fruits[0] = new Apple();
fruits[1] = new Mango();
Array Of Interface Type - Like array of class type, an array of interface type can accept any object which implements that Interface.

Accessing Array elements

We can easily get the elements inside an array by using the index of that element -
int arr[  ] = {12, 1, 20, 120};
int i = arr[2];
System.out.println(i);
above code will print the element at index 2 - 20
Loop Through an Array:
We can loop through the array elements by running the loop through its length.
We can get the array length by using length property of array - arr.length;
Example:
package com.array;

public class HelloArray {

 public static void main(String[  ] args) {
  int [  ] arr = {1,2,30,20};
  for(int i=0; i< arr.length ;i ++){
   System.out.print(arr[i]+ " ");
  }
 }
}
Output: 1 2 30 20
or we can use enhanced for loop-
Example:
package com.array;

public class HelloArray {

 public static void main(String[  ] args) {
  int [  ] arr = {1,2,30,20};
  for(int i : arr){
   System.out.print(i+" ");
  }
 }
}
This was some important and deep information about arrays in java, we can also use ArrayList in java if we don't know the exact number of elements we are going to insert inside it to store Objects. We will learn about ArrayList in our next articles. If you have any questions, feel free to ask it in the comment box below.

No comments :

Post a Comment