Java Tutorial
This java program is used to sort the arrays of integers using sort method of Arrays class.
Prints the array of numbers using for each loop.
import java.util.Arrays; public class ArraysTest { public static void main(String[] args){ int numberList[] = {3,2,7,5,1}; System.out.println("Array Sorting"); System.out.println("---------------------------"); System.out.println("Array before sorting:"); for (int number : numberList){ System.out.print(number + "\t"); } System.out.println(); Arrays.sort(numberList); System.out.println("Array after sorting:"); for (int number : numberList){ System.out.print(number + "\t"); } System.out.println(); } }Output:
D:\Java_Programs>javac ArraysTest.java D:\Java_Programs>java ArraysTest Array Sorting --------------------------- Array before sorting: 3 2 7 5 1 Array after sorting: 1 2 3 5 7
This java program is used to sort the arrays of integers using sort method of Arrays class and reverses the list of numbers using for loop.
import java.util.Arrays; public class MergeArraysTest { public static void main(String[] args){ int numberList[] = {3,2,7,5,1}; System.out.println("Array sorting and reversing"); System.out.println("---------------------------"); System.out.println("Array before sorting:"); for (int number : numberList){ System.out.print(number + "\t"); } System.out.println(); Arrays.sort(numberList); System.out.println("Array after sorting:"); for (int number : numberList){ System.out.print(number + "\t"); } System.out.println(); System.out.println("Array after reversing:"); for (int i= numberList.length-1;i>=0;i--){ System.out.print(numberList[i] + "\t"); } System.out.println(); } }Output:
D:\Java_Programs>javac ArraysTest.java D:\Java_Programs>java ArraysTest Array sorting and reversing --------------------------- Array before sorting: 3 2 7 5 1 Array after sorting: 1 2 3 5 7 Array after reversing: 7 5 3 2 1
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page