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

public class Sort_Array {

    public static void main(String[] args) {

        int a;

        Scanner sc = new Scanner(System.in);

        System.out.println(" Enter size of Array ");
        int n = sc.nextInt();

        int[] arr_num = new int[n];

        System.out.println(" Enter the integers in the Array");

        for(int i : arr_num)
        {
            arr_num[i] = sc.nextInt();
        }

        System.out.println(" Array before sorting ----\n");

        for(int j : arr_num)
        {
            System.out.print(j+",");
        }
        System.out.println("\n");

        Arrays.sort(arr_num);

        System.out.println(" Array after sorting ----\n");

        for(int k : arr_num)
        {
            System.out.print(k+",");
        }
    }
}

Output

 Enter size of Array 
2
 Enter the integers in the Array
5
6
 Array before sorting ----

6,0,

 Array after sorting ----

0,6,
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 1
    What exactly do you think `for(int i : arr_num)` does? Because you're not using it correctly. [How does primitive array work with new for each loop in Java?](https://stackoverflow.com/questions/14265888/how-does-primitive-array-work-with-new-for-each-loop-in-java) – Bernhard Barker Aug 23 '17 at 17:16

4 Answers4

0

You never initialize your array arr_num, so it will be full of zeros

    int[] arr_num = new int[n];

    ....

    for(int i : arr_num)
    {
        arr_num[i] = sc.nextInt();
    }

Will then become:

    // Pseudocode
    for(int i : {0, 0, ... , 0} )
    {
        arr_num[i] = sc.nextInt();
    }

So you will always write to arr_num[0]

You probably want to have this instead:

    for(int i = 0; i < arr_num.length; i++)
    {
        arr_num[i] = sc.nextInt();
    }
Sentry
  • 4,102
  • 2
  • 30
  • 38
0

Change this:

        for(int i : arr_num)
    {
        arr_num[i] = sc.nextInt();
    }

To a traditional for-loop and it should work.

for (int i = 0; i < arr_num.length; i++) {
     arr_num[i] = sc.nextInt();
}
Ollie in PGH
  • 2,559
  • 2
  • 16
  • 19
0

You are entering elements of array through foreach loop, but the correct way is:

System.out.println(" Enter the integers in the Array");

    /*for(int i : arr_num)
    {
        arr_num[i] = sc.nextInt();
    }*/
    for(int i=0;i<n;i++){
        arr_num[i] = sc.nextInt();
    } 
AruniAB
  • 1
  • 2
0
 System.out.println(" Enter size of Array ");
    int n = sc.nextInt();

    int[] arr_num = new int[n];

Here you see what is really happening when you have passed the size of the array say, 2

arr_num is getting initialized with 0 value ( which is the default value for int object)

0 0

After this, you have used the for each loop. Which basically says, When you see the colon (:) read it as β€œin.” So basically, you are reading each int i in array_num which are 0 0.

for(int i : arr_num)
    {
        arr_num[i] = sc.nextInt();    //arr_num[0] = sc.nextInt();
                                      //again, arr_num[0] = sc.nextInt();
    }

So this is the reason you are getting

 6,0 

because 5 which was inputted by you is now replaced by 6 again at arr_num[0]. while 2nd 0 still stays which is at arr_num[1].

As other answers suggest, use a different loop. And read more about for each loop.

Shad
  • 1,185
  • 1
  • 12
  • 27