Array concept

Array in Java  -


Array is a container where we can put the objects or values of same data type. Size of this container will be predefined and fixed length. It means when you will make the object of array then defines the length of array.
Now I am trying to explain in image.




In this figure, there is a array container with fixed 10 length. There is a value stored at 3 index in this array container. In the java context, We are writing a java program to know how value stored in java programming.

java in array Example :


public class ArrayExample{
  public static void main(String[] args) {
      // this is a array container of int data type.
      int []array = null;
      // Now fixed the length of this array container
      array = new int[10];
      //Now i have put the value in array at 3rd index.
      array[3] = 3;
      // now i am printing the value of 3rd index.
      System.out.println("3rd index value : "+array[3]);
  }
}
The output of this program will be
3rd index value : 3

If you will find the index which is greater then array length. The ArrayIndexOutOfBoundException will be occur.
If you will try to find array[10] then Exception will occur.


string arrays in java -


    Now we will try to explain string arrays in java. Like the above example, we can use the string arrays for java program.

string arrays example :


public class StringArraysExample{
  public static void main(String[] args) {
      // this is a string arrays container of String data type.
      String []stringarray = null;
      // Now fixed the length of this string array container
      stringarray = new String[10];
      //Now i have put the value in string array at 3rd index.
      stringarray[3] = "D";
      // now i am printing the value of 3rd index in array.
      System.out.println("3rd index value : "+stringarray[3]);
  }
}

output of this program is :
3rd index value : D

array of array in java:-


A array container which can hold the array object called array of array. Now I have already explain about array container.
Multidimensional array is used to hold the multiple array of same data type.
Now we will try to explain the array of array in java.

Example:-


public class ArrayOfArraysExample{
  public static void main(String[] args) {
      // this is a string arrays container of String data type.
      String []firststringarray = null;
      // Now fixed the length of this string array container
      firststringarray = new String[2];
      //Now i have put the value in string array at 3rd index.
      firststringarray[0] = "A";
      firststringarray[1] = "B";
     
      //now make another array
      String []secondstringarray = null;
      // Now fixed the length of this string array container
      secondstringarray = new String[2];
      //Now i have put the value in string array at 3rd index.
      secondstringarray[0] = "C";
      secondstringarray[1] = "D";
     
      //Array Of Arrays is defined now.           
      String [][]arrayOfArrays = {firststringarray, secondstringarray};
     
      System.out.println("at 0 index of first array :"+arrayOfArrays[0][0]);
      System.out.println("at 1 index of second array :"+arrayOfArrays[0][1]);
  }
}

output of this program is :
at 0 index of first array :A
at 1 index of second array :B

Comments

  1. well written, and very useful information is given.
    Thanks to writer

    ReplyDelete

Post a Comment

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

How to capture finger prints in java

Shallow Copy Deep Copy Java example