Thursday, 20 April 2017

Sort Array ~ foundjava

java.util.Arrays can be used to sort a array.

Example:
Sort Array

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.lang.String;
import java.util.Arrays;
public class exSortArray{
  
 public static void main(String[] args){
   
  String src[] ={
   "MNO",
   "A",
   "DEF",
   "GHI",
   "JKL",
   "PQR",
   "BC",
   "STU",
   "YZ",
   "VWX"};
   
  System.out.println("Sort Array Exerecise");
  System.out.println();
    
  System.out.println("Array before Sort");
  showAll(src);
   
  System.out.println();
   
  Arrays.sort(src);
  System.out.println("Array after Sort");
  showAll(src);
   
 }
  
 static void showAll(String[] a){
  for(int i = 0; i < a.length; i++){
   System.out.println(i + ": " + a[i]);
  }
 }
  
  

No comments:

Post a Comment