4 Arrays — Java Programming
4.1 Introduction
Arrays in Java are similar to arrays of C/C++, and have more capabilities and are more flexible, let us see how.
4.2 Arrays
An array is a collection of similar type of variables that are referred to by a common name. Arrays can be used to save any type of data.
The general form of array declaration is:
type name[ ];
Some Examples are:
int A[ ]; //single dimensional array float ab[ ][ ]; //two dimensional array
Assigning is done as follows
A[ ] = new int[9];
ab[ ][ ] = {{1,2,3,4},{5,6,7}}
;
We can also initiate elements of the array using a loop. For instance if we assign elements to array A[ ] , then the index can be used to refer to a particular element in the array
|
index |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
|
A= |
76 |
98 |
79 |
78 |
77 |
84 |
85 |
86 |
87 |
|
|
A[0] |
A[1] |
A[2] |
A[3] |
A[4] |
A[5] |
A[6] |
A[7] |
A[8] |
//findAverage.java //click here for code public class findAverage { public static void main (String[]args) { int a[] = { 76, 98, 79, 78, 77, 84, 85, 86, 89,90 }; float result = 0, avg=0; //initialise for (int i = 0; i < =9; i++) { result = result + a[i]; avg = result / 10; } System.out.println ("avg is " + avg); } } /* Output: avg is 84.2 */
Program 4.2: Find the maximum mark in a given array of marks data
//findMaximum.java //click here for code public class findMaximum { public static void main (String[]args) { int j, max = 0; int mark[] = { 88, 84, 72, 54, 67, 97, 54, 25 }; for (j = 0; j < mark.length; j++) { if (max < mark[j]) max = mark[j]; } System.out.println ("maximum: " + max); } } //Output maximum: 97
4.3 Array of objects
In array itself is an object. Just like this, we can have an array of objects. That is we assign objects of objects.
Example:
Student[] stud=new Student[3];
For creating them,
Student stud[0]=new Student; Student stud[1]=new Student; Student stud[2]=new Student;
Or, we can initialize using a for loop
for(int i=0;i<3;i++) Student stud[i]=new Student;
stud.length gives the number of elements in the array stud[ ].
Program 4.3 Program to sort an array of randomly obtained using the Math.random() method.
//sortArray.java Generate a set of 10 random numbers and sort them. //Click here for code import java.io.*; import java.util.*; public class sortArray { public static void main (String args[]) { //create an array int[] a = new int[10]; int i; for (i = 0; i < a.length; i++) a[i] = (int) (Math.random () * 100); //generate random numbers sort (a); for (i = 0; i < a.length; i++) System.out.println (a[i]); } public static void sort (int[]a) { //sort the array int n = a.length; int i, j, k; boolean flag; flag = false; for (i = 0; i < n; i++) { for (j = 0; j < n - 1; j++) { if (a[j] > a[j + 1]) { k = a[j]; a[j] = a[j + 1]; a[j + 1] = k; flag = true; } } if (!flag) break; } } } /* Output: 1 27 33 41 45 54 62 75 87 95 */
4.4 Java’s arrays are different from other language arrays
All programming languages support arrays. A Java array is guaranteed to be initialized