By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Sort Array of Numbers - Java Task

+5 votes

Write a program to enter a number n and n integer numbers and sort and print them. Keep the numbers in array of integers: int[].


sort an array of numbers example table

asked in Java category by user eiorgert
edited by user golearnweb

1 Answer

+1 vote
Best answer

Here's my solution (the most important line here is the 17th - by using Arrays's class method sort):

import java.util.Arrays;
import java.util.Scanner;

public class Pr_01_SortArrayOfNumbers {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(;

        int n = scanner.nextInt();

        int[] array = new int[n];

        for (int i = 0; i < n; i++) {
            array[i] = scanner.nextInt();


        System.out.println("Printing with foreach:");
        for (Integer number : array) {
            System.out.print(number + " ");


        System.out.println("Printing with for:");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");


answered by user hues
selected by user golearnweb