-4

Problem: when printing a sorted array with data type "int" it is returning funny character "[I@edf4efb". It looks like the address of the array

Question: how to print a sorted array?

Code listed below:

import java.util.Arrays;

public static void main(String[] args) {
  int [] sa = {8,200,10};
  Arrays.sort(sa);
  Arrays.sort(sa);
  System.out.println(sa);
}

Actual result: [I@edf4efb

Expected result: 8, 10, 200

ayuicyi
  • 13
  • 7

2 Answers2

1

Use Arrays toString()

Arrays.toString()

System.out.println(Arrays.toString(income));
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
  • Anyone can help me with my extended question so that I can understand fully on "why do we need to use for loop to print out a sorted array? However, I don't need a for loop to print a specific index from an array, though... example `int [] sa = {8,200,10};` `System.out.println("The 2nd index for sa array is: " + sa[1]);` kindly help thanks. – ayuicyi Dec 13 '19 at 10:30
0

Corrected programme

public static void main(String[] args) {
        int[] sa = { 8, 200, 10 };
        Arrays.sort(sa);
        for (int i = 0; i < sa.length; i++) {
            System.out.println(sa[i]);
        }
    }
tarun
  • 218
  • 2
  • 11
  • Thank you all. @tarun, I have not pick up "for" loop tutorial yet. so whey do we need to use for loop to print out a sorted array? for an example, if i want to print a specific index from an array, i dont have to need to use it though... example `int [] sa = {8,200,10};` `System.out.println("The 2nd index for sa array is: " + sa[1]);` – ayuicyi Dec 13 '19 at 05:51