7.1 Introduction to Array

An array is a group of values having same datatype. Each value in array is identiﬁed by an index. To create an array in Java, you must do the following:

1. Declare a variable to hold the array.

2. Create a new array object and assign it to the array variable.

3. Store information in that array.

Declaring Array Variables

The following statements are examples of array variable declarations:



int[] list;

String[] fruits;

Rectangle[] boxes;

Creating Array Object

After you declare the array variable, the next step is to create an array object and assign it to that variable, as in the following statement :

list = new int[10];

fruits = new String[5];

boxes = new Rectangle[4];

Declaring array variable and creating array object can be done in single statement, as given :

int marks = new int[5];

You can create and initialize an array at the same time by enclosing the elements of the array inside braces, separated by commas:

double[] prices = {10.5, 20.1, 14, 39};

The above statement creates a three-element array of double values named prices.

Accessing elements



You can accesse array element with the array name followed by a subscript enclosed within square brackets, as in the following:



marks[4] = 30; //stores 30 in fifth element of array.

Example 1

import java.util.Scanner; //Needed for Scanner class /** * This program shows values being read into an array's * elements and then displayed. */ public class ArrayDemo1 { public static void main(String[] args) { final int SIZE = 5; // size of array // Create an array to hold employee salary. double[] salaries = new double[SIZE]; // Create a Scanner object for keyboard input. Scanner console = new Scanner(System.in); System.out.println("Enter the salaries of " + SIZE + " employees."); // Get employees' salary. for (int i = 0; i < SIZE; i++) { salaries[i] = console.nextDouble(); } // Display the values stored in the array. System.out.println("The salaries are:"); for (int i = 0; i < SIZE; i++) { System.out.println(salaries[i]); } } }

Output : Enter the salaries of 5 employees.

234

789

123

456

453

The salaries are:

234.0

789.0

123.0

456.0

453.0

Specifying Array Size during Program Execution

During program execution, you can first prompt the user to specify the size of the array and then instantiate the object. Look at the following statements :



int size; System.out.print("Enter the size of the array: "); size = console.nextInt(); //number of elements in array is the value of the variable size int[] list = new int[size];

Array length

All arrays have one named instance variable: length that contains the length of the array (number of elements). You may use this value as the upper bound of a loop, rather than a constant value.



Example 2

import java.util.Scanner; //Needed for Scanner class /** * This program shows values being read into an array's * elements and then displayed its sum */ public class ArrayDemo2 { public static void main(String[] args) { int size; // Create a Scanner object for keyboard input. Scanner console = new Scanner(System.in); System.out.print("Enter the size of the array: "); size = console.nextInt(); // Create an array to hold integers. int[] list = new int[size]; System.out.println("Enter " + list.length + " integers."); // Get integers. for (int i = 0; i < list.length; i++) { list[i] = console.nextInt(); } int sum = 0; // To hold total // Calculate the sum of elements. for (int i = 0; i < list.length; i++) { sum += list[i]; } // Display the sum of array elements. System.out.println("The sum of array :" + sum); } }