0

What is wrong with this code I'm getting wrong output. I don't know what's wrong, I hope you could help me:

public class Main{
  public static void main(String[] args){
    int[] data={11,22,33,55,22,88,99,77};

    SortingAlgo algo=new SortingAlgo();
    data=algo.selectionSort(data);
    System.out.println("numbers are"+ data);
  }
}

Other class

public class SortingAlgo{
  public int[] selectionSort(int[] data){
    int lenD = data.length;
    int j = 0;
    int tmp = 0;
    for(int i=0;i<lenD;i++){
      j = i;
      for(int k = i;k<lenD;k++){
        if(data[j]>data[k]){
          j = k;
        }
      }
      tmp = data[i];
      data[i] = data[j];
      data[j] = tmp;
    }
      return data;
  }
}

This is my out put:

numbers are[I@2e4b1dd8
1
  • 3
    That output is correct. Your expectations are wrong. Look into Object#toString(). Commented Oct 2, 2013 at 13:59

3 Answers 3

7

The output is completely fine. The arrays don't override toString() method, so it invokes the Object#toString() method, which generates that kind of representation. The output is of the form:

getClass().getName() + '@' + Integer.toHexString(hashCode())

For arrays, the Class#getName() method uses some encoding for different element type to generate unique class name. The encoding rule is specified in the documentation.

To get the human readable representation, you can use Arrays#toString() method:

System.out.println("numbers are"+ Arrays.toString(data));
Sign up to request clarification or add additional context in comments.

Comments

4

data is an array of ints. You should use Arrays#toString(), which is implemented this way:

3860     public static String toString(int[] a) { {
3861        if (a == null)
3862            return "null";
3863        int iMax = a.length - 1;
3864        if (iMax == -1)
3865            return "[]";
3866
3867        StringBuilder b = new StringBuilder();
3868        b.append('[');
3869        for (int i = 0; ; i++) {
3870            b.append(a[i]);
3871            if (i == iMax)
3872                return b.append(']').toString();
3873            b.append(", ");
3874        }
3875    }

Make sure that you understand it, it will help you to understand arrays.

You can loop on the array and manually print it:

for(int i: data) {
    System.out.println(i + " ");
}

This way you have control on which values to print, e.g. even values:

for(int i: data) {
    if(i % 2 == 0) {
        System.out.println(i + " ");
    }
}

Regarding the output you are getting, this is the explanation about it:

In Java, each object has toString() method, the default is displaying the class name representation, then adding @ and then the hashcode.

Comments

4

The toString() for arrays is broken. You need

import java.util.Arrays;

System.out.println("numbers are" + Arrays.toString(data));

The same applies for Arrays.equals(), Arrays.hashCode(). The Array, Arrays, ArrayUtils classes add functionality you might like arrays to have.

http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Array.html

http://docs.oracle.com/javase/7/docs/api/java/sql/Array.html

http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/ArrayUtils.html

However, you may find that you really want ArrayList instead.

9 Comments

I wouldn't call it broken. It's just not implemented. Intentionally I guess.
@stonedsquirrel maybe there is a good reason I would rather get [I@2e4b1dd8 than the values, but I doubt it. IMHO Its broken as there is no class for int[] to sub-class this behaviour.
One I could think of would be performance/output size. Not everytime toString() is called it is clear on what object type it is called. Having a huge array could kill performance in such a case or make a log file unreadable.
File: C:\Java\drjava\Main.java [line: 9] Error: cannot find symbol symbol: variable Arrays location: class Main
its fixed now thanks :D i misstyped the import. thank you very much
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.