Uses of Array in Java
Java store list of values in arrays. An array is a group of contiguous (near) memory locations that all have the same name and the same type. To refer to a specific locations or element within the array, we specify the name of the array and position number of the appropriate elements in the array.
Name of array
Note:- All the elements of this array have the same name a
|
|
Position number [1]....[11] of the element within array a
Examples Using Array
public class InitArray {
public static void main(String[] args) {
String output = "";
int n[]; // declares reference of array
n = new int[10]; //dynamically allocate array
output += "subscript\tvaluye\n";
for (int i = 0; i < n.length; i++)
output += i + "\t" + n[i] + "\n";
System.out.println(output);
}
}
Output
subscript value
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
//initalize array n to the even integers from 2 to 20
public class InitArray
{
public static void main (String[] args)
{
final int ARRAY_SIZE = 10;
int n []; //reference of int array
String output = "";
n = new int [ARRAY_SIZE]; //allocate array
for (int i = 0; i < n.length; i++)
n[i]= 2+2*i;
output += "subscript\tvalue\n";
for (int i = 0; i < n.length; i++)
output += i +"\t" + n[i] + "\n";
System.out.println( output );
}
}
Output
subscript value
0 2
1 4
2 6
3 8
4 10
5 12
6 14
7 16
8 18
9 20
Declaring and Allocating Arrays
Arrays use space in memory. The programmer establish the type of elements and uses operator new to dynamically allocate the number of elements required by each array.
Array are allocated with new because array are treated to be object and all the objects must be build with new. To allocate 12 elements for integer array a, the declaration
int a [ ] = new int [14];
The statement can be performed in two steps are as follow:-
int a []; //declares the array
a = new int[13]; //allocate the array
When we declare an array, the type of array and the square brackets can be combined at the beginning of the declaration to indicate that all identifieres in the declaration show as array
double[] array1, array2;
The elements of an array can be initialized by declaration (by using the initializer lists), by position and by input.
Note:- Array may be consist of any data type . It is important to recall that in an array of primitive data type, every element of the array contains one value of the declared data type of the array.
Passing Array to Methods
To pass an array argument to a method ,mention the name of array without any brackets.
int weekDays[] = new int[7];
// The method call
modifyArray (weekDays);
The array pass weekDays to method modifyArray. In Java every array object "knows" for its own size . Thus ,when we pass an arrya object into a method we do not independently pass the size of the array of the argument.
Sorting Arrays
Sorting data i.e placing the data into some particular order such as asecending or descending is one of the most important applications.Sorting data is an compelling problem that has engage some of the most extraordinary research efforts in the field of computer science
eg Telephone companies sort their list of account by the last name and within that , by the first name to make it easy to find phone numbers . Basically every organization must sort some data and in may cases heavy amounts of data.
Example:-
import java.util.Arrays;
import java.util.Random;
public class Sort
{
public static void main(String[] args)
{
Student[] students = getStudents();
System.out.println(Arrays.toString(students)); //Unsorted array
Arrays.sort(students);
System.out.println(Arrays.toString(students)); //Sorted array
}
//Some method which return array of students in random order
private static Student[] getStudents()
{
Random rand = new Random(10);
Student[] arr = new Student[5];
for(int x = 0; x < 5; x++) {
Student s = new Student();
s.setId(rand.nextInt(100));
arr[x] = s;
}
return arr;
}
}
Output
[Student [id=13], Student [id=80], Student [id=93], Student [id=90], Student [id=46]]
[Student [id=13], Student [id=46], Student [id=80], Student [id=90], Student [id=93]]
In the above example sorts the value of the 5-element array a into ascending order. This technique is called a bubble sort or sinking sort array, it is travel from first element to last element. On this place the, current element is distinguished with the next element. If current element is greater than the next element, it is exchange.
Multiple-Subscripted Array
Array may be used to show tables of value consisting of information organize in rows and column .To analyze a particular element of the table ,two subscripts are described.The first organize the row in which the element is include and the second identifies with the column in which the element is contained
Table or array that requires two subscripts to identify a specific element are called double subscripted arrays.
Column 0 | Column 1 | Column 2 | Coumn 3 | |
Row 0 | a [0] [0] | a [0] [1] | a [0] [2] | a [0] [3] |
Row 1 | a [1] [0] | a [1] [1] | a [1] [2] | a [1] [3] |
Row 2 | a [2] [0] | a [2] [1] | a [2] [2] | a [2] [3] |
a (array name) [2] ( Row subscript) [1] (column subscript)