Java One Dimensional Arrays

Java One Dimensional Arrays

Declaring an array is the same way of declaring a variable but with an extra square brackets. To inform the compiler, that the variable represents an array, suffix or prefix (Java allows) the variable name with a pair of square brackets. Array size should be given at the time of creation itself. Depending upon the size, continuous memory locations are allocated to the elements. Array identifier follows the same rules of Java identifiers.

Syntax of Java One Dimensional Arrays

Declaring, Assigning and Accessing the Array

The following program explains the basic syntax of creating an array in Java. Let us start with one-dimensional array. First an integer array by name, marks, is created which is filled with zero values by default. Later values are given (not from keyboard for which you must wait until I/O streams). The elements are retrieved using a for loop and printed.

Example on Java One Dimensional Arrays
public class ArrayTest1
{
  public static void main(String args[])
  {
    int marks[] = new int[5];
	
    System.out.println("Elements are " + marks.length);            
    System.out.println("Default value: " + marks[0]);            
			                               // Now assign values
    marks[0] = 50;
    marks[1] = 60;
    marks[2] = 70;
    marks[3] = 80;
    marks[4] = 90;

    System.out.println("Value of 1st element: " + marks[0]);            

    System.out.println("\nPrint the values in a single row");
    for(int i = 0; i < marks.length; i++)
    {
	System.out.print(marks[i] + "\t");  // to print in a single line, ln is removed
    }

    System.out.println("\n\nOther way of printing supported from JDK 1.5");
    for(int k : marks)
    {
        System.out.print(k + "\t");
    }    
  }
}  
Java One Dimensional Arrays
Output screen of ArrayTest1.java

int marks[] = new int[5];

The above style of declaration is just the same as C/C++. The couple of square brackets indicate that marks is an array. In Java, arrays are predefined objects. The marks array can store 5 elements of integer values. Following is the schematic representation of the above array.

One D Array

Java supports another style of declaration

int[] marks = new int[5];

The array variable, marks, is prefixed (not suffixed) with square brackets.

Infact, the above statement can be modified into two statements – declaration and assignment.

int marks[];
marks = new int[5];

In the first statement, array by name marks is declared and in the second statement its size is assigned. The JVM allocates memory of 5 locations of size 4 bytes each; total 20 bytes.

System.out.println("Number of elements : " + marks.length);

The length variable of array gives the number elements present in the array. Arrays are treated internally as objects by JRE.

System.out.println("Default value: " + marks[0]);

marks[0] returns the value of the first element. It prints zero as no value is assigned with still. Unassigned values are filled with default values, but not with garbage values.

          for(int k : marks)
          {
              System.out.print(k + "\t");
          }    

The above for loop is a strange one supported from JDK 1.5 onwards and works with arrays and data structures only. It is a simplified version of general for loop.

3 thoughts on “Java One Dimensional Arrays”

  1. Sir for the above program i write this statement “System.out.println(“fifth element value is ” + pavan[5]);” it is throwing the exception as Array Index out of bound exception but i didn’t write any try catch here who trowed this exception?

Leave a Comment

Your email address will not be published.