Java Arrays - Step By Step To Understand Array

This tutorial will take you step by step through the process of understanding and using arrays. The best way to learn is to compile and run these programs yourself (copy, paste, compile and run !). Comments such as /* this is a comment */ or // this is another comment are inserted to explain what does the line of code do. The programs are kept simple for the purpose of concentrating on the main idea in question.

Example 1 : declaring and initializing 1-dimensional arrays

An array groups elements of the same type. It makes it easy to manipulate the information contained in them.

  

class Arrays1{ 

   public static void main(String args[]){

// this declares an array named x with the type "array of int" and of 
// size 10, meaning 10 elements, x[0], x[1] , ... , x[9] ; the first term
// is x[0] and the last term x[9] NOT x[10].
      int x[] = new int[10];

// print out the values of x[i] and they are all equal to 0.
      for(int i=0; i<=9; i++)
         System.out.println("x["+i+"] = "+x[i]);

// assign values to x[i] 
      for(int i=0; i<=9; i++)
         x[i] = i;  // for example
       
// print the assigned values of x[i] : 1,2,......,9
      for(int i=0; i<=9; i++)
         System.out.println("x["+i+"] = "+x[i]);

// this declares an array named st the type "array of String"
// and initializes it
      String st[]={"first","second","third"};

// print out st[i]
      for(int i=0; i<=2; i++)
         System.out.println("st["+i+"] = "+st[i]);

    }
}

Check that the output corresponds to what is expected.

Example 2 : Find the sum of the numbers 2.5, 4.5, 8.9, 5.0 and 8.9

  
class Arrays2{ 

   public static void main(String args[]){

// this declares an array named fl with the type "array of int" and
// initialize its elements

      float fl[] = {2.5f, 4.5f, 8.9f, 5.0f, 8.9f};

// find the sum by adding all elements of the array fl
      float sum = 0.0f;
      for(int i=0; i<= 4; i++)
         sum = sum + fl[i];

// displays the sum
         System.out.println("sum = "+sum);
    }
}

Check that the sum displayed is 29.8.

Example 3 : declaring and initializing 2-dimensional arrays

  

class Arrays3{ 

   public static void main(String args[]){

// this declares a 2-dimensional array named x[i][j] of size 4 (4 elements)
// its elements are x[0][0], x[0][1], x[1][0] and x[1][1].
// the first index i indicates the row and the second index indicates the
// column if you think of this array as a matrix.


      int x[][] = new int[2][2];

// print out the values of x[i][j] and they are all equal to 0.0.
      for(int i=0; i<=1; i++)
        for(int j=0; j<=1; j++)
         System.out.println("x["+i+","+j+"] = "+x[i][j]);

// assign values to x[i] 
      for(int i=0; i<=1; i++)
        for(int j=0; j<=1; j++)
         x[i][j] = i+j;  // for example
       
// print the assigned values to x[i][j] 
      for(int i=0; i<=1; i++)
       for(int j=0; j<=1; j++)
         System.out.println("x["+i+","+j+"] = "+x[i][j]);

// this declares a 2-dimensional array of type String
// and initializes it
      String st[][]={{"row 0 column 0","row 0 column 1"}, // first row 
                        {"row 1 column 0","row 1 column 1"}}; // second row 

// print out st[i]
     for(int i=0; i<=1; i++)
       for(int j=0; j<=1; j++)
        System.out.println("st["+i+","+j+"] = "+st[i][j]);

    }
}
other ways of declaring arrays
  
        int[] x = new int[2];
        double []d[] = new double[1][3];
        String [][]st = new String[3][6];

Related:

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.