ONE DIMENSIONAL ARRAY

One Dimensional Array


    Arrays is a one of the class in java.utils package. It can store (or) hold multiple similar data values.

    Arrays can hold either primitive values (or) derived values. Incase of primitives it holds same type of primitive values.

    But in case of derived type of arrays it can hold same type (or) sub type of derived type.

    There is no separate primitive type array (or) derived type array.

How to declare an array:

  • If we want to declare any array we should require one data type that is either primitive or derived type. Then symbol of array is  "[ ]" followed by identifier.


How to declare multi dimension array:

How to initialization of Array:


      // here [10] is called as Array Dimension
      // CTE it is not possible. here ‘I’ is not primitive int var ,it is Array object of int type. Array’s are derived types.
      //it is possible, we can assign ‘null’ value to the derived data type ref var.


    ­» For Array we can add element value corresponding to the data type.

Example

String s1[ ] = new String[5];

­» It will create one String array object with 5 element capacity. and index is 0 to n-1 means 0 to 4. ,In arrays by default each position will create With corresponding default values i.e. int means 0 and String means null etc….

    javapadho
    Arrays Example

    If we create one int array
    int i[ ] = new int[5]; it will create array object initially like below

    javapadho
    Arrays Example

­» Creating object for array type with string type elements.

    Example 1

    String s2[ ]=new String();

     Here Compilation fails why because we can’t assign String class object to String Array type ref variable.

    Example 2

    String s3[ ]=String[5]; //CTE

     Compilation fail why because we didn’t use “new” operator

    Example 3

    String s1[ ] = new String[5];
    String s4[ ]=s1; // C.S

     Compilation success why because s1 is a string type array we can also assign the string type array reference variable.

    Example 4

    String s5=s1;

     Compilation fail why because s1 is a string type array so we can’t assign to normal string reference variable

How to populating data to Array:

  • First we should declare and initialize an array. After that go for populating array.

  • Manipulation of an array depends on index value.

Example

int i[ ];  // declaration
int i[ ];  // initialization

 // manipulating data into arrays. i[0]=20;
i[1]=30;
i[2]=40;
i[3]=50;
i[4]=60;
    javapadho
    manipulating data into arrays
    javapadho
    manipulating data into arrays
    Why Because
  • index should be > 0 and <= length-1 .

  • If we want to find length of the Array we should use length attribute in array class. It will give total number of elements in an array.

  • Example

    System.out.println(i.length()); // here we will get output as 5. So max index is 5-1 = 4. Min index is = 0. If we exceed the range means <0 or >5-1 then we will get ArrayIndexOutOfBoundException.


Example 1

    s1[0] = ”abc”;
    s1[1] = ”ram”;
    s1[2] = new String(“raj”);
    s1[3] = new Integer(“30”); // CTE

    in the above line we will get Compilation fail why because incompatible type, we are trying to assign integer value to String type array element. It’s not possible.
    But we can use like below line its works fine

    s1[4] = new Integer(“30”).toString(); it is equaling to s1[4]=”30”;

    • In case of derived type object arrays can hold only object reference but not original object value.

    • S1 is reference variable to array. S1[0] is reference variable to the value of string object “abc”.

Example 2

    String s1[]=null;
    s1[0]=”abc”; // RTE i.eNullPointerException .

    • Why because s1 array is initialized with null object , But we try to assign values at s[0] position , it’s not possible,

    • If we try to do any operation on null object we will get java.lang.NullPointerException

How to manipulation the Arrays:


1.By using direct index manualy:

  • If we want to manipulate the arrays we should use the index must and should. the index should be between 0 to n-1 (here n specifies the length of array object.),

  • if we exceed that index range we will get ArrayIndexOutOfBoundException at runtime.


  • Example :
      //declaring and initializing the array
      String s1[ ]=new String[3];

      // now manipulate the data into array.
      s1[0] = ”abc”;

      s1[1] = ”xyz”;

      s1[2] = ”30”;

      // Reading data from Array

  • if you want read 0th index value from string array. then do like

  •   String s2 = s2[0];

      System.out.println(s2[0]); // output is: abc

  • datatype of array specifies which type of data it can hold and which type of data it can return.

  •   int I = s2[1]; // here will get CTE.

    Why because s2 is String type array, so it contains only String object values. But we are trying to assign 1st position index string value to int type ref Variable which isn’t possible. If you want to make compile success use like below.

      String str1 = s2[1];

  • Index of array should be between 0 to n-1.

  •   System.out.println(s2[3]); // we wiil get RTE.

    Why because s2 String array contain length is 3, so index should be 0 to (3-1) = 0 to 2. But we are trying to read 3rd index position value from s2. If index range exceeds we will get ArrayIndexOutOfException.

How to Iterate Arrays by using for loop:


    if we want to iterate array elements through for loop dynamically. Then see the below example.

    Example 1:

     
                    
                 class ArrayDemo 
                 {
                    public static void main(String []args)
                    {
                       String s1[] = new String[3];
                       s1[0]="abc";
                       s1[1]="abc";
                       s1[2]="abc";
                       for( int i=0; i < s2.length; i++)
                       {
                          String s=s2[i];
                          System.out.println("Value in:"+i+"index position is: "+s);
                       }
                    }
                 }                

    Output:

    Value in:0 index position is : abc Value in:1 index position is : xyz Value in:2 index position is : 30

How to Iterate Array by using while loop:


    For iterating array with while loop see below example.

    Example 2:

     
                    
                 class  ArraysDemo
                 {
                    public static void main( String[ ] args)    
                    {
                       String s1[ ]=new String[3];
                       s1[0]=”abc”;
                       s1[1]=”xyz”;
                       s1[2]=”30”;
                       if ( s2 != null ) 
                       {
                          while( i < s2.length )
                          {
                             System.out.println(“Value in”+i+”index position is :”+s2[i]);
                             i++ ;
                          }
                       }
                    }
                 }                

    Output:

    Value in:0 index position is : abc Value in:1 index position is : xyz Value in:2 index position is : 30

Annonymous Arrays:


    If we create any Array without name is called as “Anonymous Array”. It is shown in example2.

    Example 1:

      javapadho
      Annonymous Arrays

    Example 2:

      javapadho
      Annonymous Arrays

    This Anonymous is equal to above concrete array which are same.