Java Arrays

Java Arrays

An array is a data structure which will store only homogeneous elements.That means all the elements stored in an array would be of same data type.

For example if we create an array of string then all the elements which can be stored would be of type String only.Other data types like int,long or double or object can not be stored in that array.

Features of an Array:

1)Elements of array can be accessed by the index like zeroth element,first element,second element and so on.

2)Array can be think of as a group for all similar elements since all elements can be accessed by same name,only index will be different.

3)Elements will be stored in contiguous location, means elements in the arrays are stored at adjacent positions.

However there are 2 constraints of Arrays.
1) We can’t change the size of Array once it is declared since we need to provide the size of the elements while declaring it.To overcome this problem java has concept of ArrayList.
2)As mentioned above we can’t store heterogeneous type of data, like few string elements alongwith the other data types like int or double.
 
Array Declaration:
First we need to provide data type means what type of data will be stored in an array, followed by the array name.
double[] priceArray;
Here we have created an array of data type double,array name is priceArray.So in this array we can store data of type double but question is how many elements can be stored.
 
That we will provide by allocating memory.
Memory Allocation for array:
priceArray = new double[10];
 
Now our array is ready to use.We can store 10 elements of type double.
 
Array Initialization:
 
priceArray[0] = 250.50;
priceArray[1] = 50.50;
priceArray[2] = 150.50;
priceArray[3] = 350.50;
 
Now array has been initialised with 4 elements.As we mentioned earlier, elements can be stored and accessed by changing the index for the array.Here index value is 0,1,2 and 3
 
Accessing Array:
 
System.out.println(“First Element: ” + priceArray[0]);
System.out.println(“Second Element: ” + priceArray[1]);
System.out.println(“Third Element: ” + priceArray[2]);
System.out.println(“Fourth Element: ” + priceArray[3]);
 
 
Looping through array elements:
Here we will see 2 ways of iterating the array elements.First is before java 8 and another one will be for-each loop introduced in java8
 
1) Using for loop
 
class TestArray {
 public static void main(String[] args) {
  
   // create an array
   int[] priceArray = {250.50,340.50,560.00,230.60};
 
   for(int i = 0; i < priceArray.length; i++) {
     System.out.println(priceArray[i]);
   }
 }
}
 
2)Using the for-each Loop:
 
 
class TestArray {
 public static void main(String[] args) {
  
   // create an array
 int[] priceArray = {250.50,340.50,560.00,230.60};
 
 
   // using for-each loop
 
   for(int a : priceArray) {
     System.out.println(a);
   }
 }
}
 
Arrays Sorting:
Arrays sort is used for sorting elements in an array.There are 2 variations of sort method
 
1) Which does not take any input for sorting location and sorts all the elements of the array.
2) Another variation of sort takes the starting and last index of the array and sort the elements within that range only.
 
 Example 1: Which does not take any index


import java.util.Arrays;

public class ArraySortTest {
public static void main(String args[]) {

int[] arrayOfIntegers = {15, -22, 33, 17, 387, -78, 678};
System.out.println(“Array before sorting : “);
for (int num : arrayOfIntegers) {
System.out.print(num + ” ,”);
}

Arrays.sort(arrayOfIntegers);

System.out.println(“nArray before sorting : “);

for (int num : arrayOfIntegers) {
System.out.print(num + ” , “);
}

}
}

Output:


Array before sorting :
15 ,-22 ,33 ,17 ,387 ,-78 ,678 ,
Array before sorting :
-78 , -22 , 15 , 17 , 33 , 387 , 678 ,



 Example 2: Which takes starting and last index and sort within that range


import java.util.Arrays;

public class Test {
public static void main(String args[]) {

int[] arrayOfIntegers = {15, -22, 33, 17, 387, -78, 678};
System.out.println(“Array before sorting : “);
for (int num : arrayOfIntegers) {
System.out.print(num + ” ,”);
}

Arrays.sort(arrayOfIntegers, 1, 3);

System.out.println(“nArray before sorting : “);

for (int num : arrayOfIntegers) {
System.out.print(num + ” , “);
}

}
}

Output:


Array before sorting :
15 ,-22 ,33 ,17 ,387 ,-78 ,678 ,
Array before sorting :
15 , -22 , 33 , 17 , 387 , -78 , 678 ,

Leave a comment