Array Example - Array in Java

Array: Array is the most important thing in any programming language.
By definition, array is the static memory allocation. It allocates the memory
for the same data type in sequence. It contains multiple values of same types. It also store the values in memory at the fixed size.
Multiple types of arrays are used in any programming language such as: one - dimensional,
two - dimensional or can say multi - dimensional.
Declaration of an array:
int num[]; or int num = new
int[2];
Some times user declares an array and it's size simultaneously. You may or may not be define the
size in the declaration time. such as:
int num[] = {50,20,45,82,25,63};
In this program we will see how to declare and implementation. This program illustrates
that the array working way. This program takes the numbers present in the num[]
array in unordered list and prints numbers in ascending order. In this program
the sort() function of the java.util.*;
package is using to sort all the numbers present in the num[]
array. The Arrays.sort() automatically sorts the
list of number in ascending order by default. This function held the argument
which is the array name num.
Here is the code of the program:-
import java.util.*;
public class array{
public static void main(String[] args){
int num[] = {50,20,45,82,25,63};
int l = num.length;
int i,j,t;
System.out.print("Given number : ");
for (i = 0;i < l;i++ ){
System.out.print(" " + num[i]);
}
System.out.println("\n");
System.out.print("Accending order number : ");
Arrays.sort(num);
for(i = 0;i < l;i++){
System.out.print(" " + num[i]);
}
}
}
|
Output of the program:
|
C:\chandan>javac array.java
C:\chandan>java array
Given number : 50 20 45 82 25 63
Ascending order number : 20 25 45 50 63 82
|

|