/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
/**
*
* @authorTruffy
*/
class ArrayDemo {
public void forsortIntArray() {
int[] arrayToSort = new int[] {100, 200,300, 400, 500, 600, 900, 800, 700};
Arrays.sort(arrayToSort);
for (int i = 0; i < arrayToSort.length; i++)
System.out.println(arrayToSort[i]);
}
public void doLoopsortIntArray() {
int[] arrayToSort = new int[] {100, 200,300, 400, 500, 600, 900, 800, 700};
Arrays.sort(arrayToSort);
int loopvar = 0; // Declare and initialize the loop counter
do {
System.out.println(arrayToSort[loopvar]); // Print the variable
loopvar = loopvar + 1; // Increment the loop counter
} while (loopvar < 9); // Test and loop
}
public void whileLoopsortIntArray() {
int[] arrayToSort = new int[] {100, 200,300, 400, 500, 600, 900, 800, 700};
Arrays.sort(arrayToSort);
int loopvar = 0; // Declare and initialize the loop counter
while (loopvar < 9){
System.out.println(arrayToSort[loopvar]);
loopvar = loopvar + 1;
}
}
public void sortIntArray() {
int[] intArray = new int[] {100, 200,300, 400, 500, 600, 900, 800, 700};
// Arrays.sort(intArray);
// translate int array over to an Integer Array
Integer[] integerArray = new Integer[intArray.length];
for (int i = 0; i < intArray.length; i++)
{
integerArray[i] = new Integer(intArray[i]);
}
// sort with an anonymous Comparator object
Arrays.sort(integerArray, new Comparator<Integer>()
{
public int compare(Integer o1, Integer o2)
{
return o2.intValue() - o1.intValue();
}
});
System.out.println(Arrays.toString(integerArray));
}
public static void main(String[] args) {
ArrayDemo arr = new ArrayDemo();
System.out.println("[1] Display contents of the array using for loop (by index, ascending)");
System.out.println("[2] Display contents of the array using do loop (by index, ascending)");
System.out.println("[3] Display contents of the array using while loop (by index, ascending)");
System.out.println("[4] Display contents of the array using value (by index, descending)");
System.out.println("[5] Exit\n");
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String input = sc.next();
if(input.equals("5")){
System.out.println("Bye! Bye!");
System.exit(0);
}else if(input.equals("1")){
arr.forsortIntArray();
}else if(input.equals("2")){
arr.doLoopsortIntArray();
}else if(input.equals("3")){
arr.whileLoopsortIntArray();
}else if(input.equals("4")){
arr.sortIntArray();
}
System.out.println("Enter cmd: ");
}
}
}