Array in Java

 

Array in Java

Array is a simple Data Structure as same as which we have read in C, C++. If we need to describe then, "Array is a container which holds fixed no. of values of 1 data type (ie., If its Integer array then we can't store Character or String in it)". Size of the array must be defined at the time of array creation and its fixed until and unless if we try to re-create same array with different size. Lets see simple String array creation example in java.


public class ArrayTest {
 
 public static void main(String[] args) {
  
  // Creating Array with size 5
  String[] array = new String[5];
  
  array[0] = "java";
  array[1] = "J2EE";
  array[2] = "Servlet";
  array[3] = "JSP";
  array[4] = "Web Services";
  
  // Which gives runtime exception
  // ArrayIndexOutOfBoundsException
  //array[5] = "java";
  
  System.out.println("array[0] - "+array[0]);
  System.out.println("array[1] - "+array[1]);
  System.out.println("array[2] - "+array[2]);
  System.out.println("array[3] - "+array[3]);
  System.out.println("array[4] - "+array[4]);
 }
}


OUTPUT: 


array[0] - java
array[1] - J2EE
array[2] - Servlet
array[3] - JSP
array[4] - Web Services


As we seen in above example we can't assign values to "array[5]", since we have created array with the size of 5 and we are trying to store value beyond its size. So we will get run-time exception call ArrayIndexOutOfBoundsException.


Multi-Dimensional Array:

Above we have seen about single dimensional array creation with example. In Java we can use Multi-Dimensional array also as like below.


public class ArrayTest {
 
 public static void main(String[] args) {
  
  String names[][] = { {"thread", "array", "collection"}, 
        {"jsp", "servlet"}, 
        {"SOAP", "RESTFull"} 
          };
  
  System.out.println("names[0][0] - "+ names[0][0]);
  System.out.println("names[1][1] - "+ names[1][1]);
  System.out.println("names[2][0] - "+ names[2][0]);
  System.out.println("names[0][3] - "+ names[0][2]);
  
 } 
}


OUTPUT:


names[0][0] - thread
names[1][1] - servlet
names[2][0] - SOAP
names[0][3] - collection



Array Types:

Not only String we can create array with all primitve and non-primitive data types in Java as like below. 


int[] _int = new int[5];
char[] _char = new char[2];
float[] _flat = new float[4];
Integer[] _Integer = new Integer[5];
Boolean[] _Boolean = new Boolean[4];




Copying array values to another array:

System class method arraycopy() is used to copy array values from 1 array (source) to another array (destination). Both source and destination array must be same data type. arraycopy() method contains 5 parameters like
src - Source array
srcPos - Source array position to copy
dest - Destination array
destPos - Destination array position to be copied 
length - Length of array to copy 

Below is a simple example to copy array value from 1 array to another array.


public class ArrayTest {
 
 public static void main(String[] args) {
  
  int[] _intSrc = new int[]{0,11,12,13,14,15,16,17,18,19};
  
  int[] _intDes = new int[5];
  
  _intDes[0] = 99;
  
  // Copying values from 11 to 14
  System.arraycopy(_intSrc, 1, _intDes, 1, 4);
  
  for (int val : _intDes) {
   System.out.println(val);
  }
 } 
}


OUTPUT:


99
11
12
13
14



Another way of copying values from 1 array to another array using java.util.Arrays() class. Method called copyOfRange() used to copy specific array elements to another array same like below example code.


import java.util.Arrays;

public class ArrayTest {
 
 public static void main(String[] args) {
  
  int[] _intSrc = new int[]{0,11,12,13,14,15,16,17,18,19};
  
  // Copying from _intSrc (Range index 2 to 7)
  int[] _intDes = Arrays.copyOfRange(_intSrc, 2, 7);
  
  for (int val : _intDes) {
   System.out.println(val);
  }
 } 
}


OUTPUT:


12
13
14
15
16










No comments:
Write comments