打印阵列显示错误输出 [英] Printing array shows wrong output

查看:90
本文介绍了打印阵列显示错误输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码出了什么问题我输错了。我不知道什么是错的,我希望你能帮助我:

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);
  }
}

其他类

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;
  }
}

这是我的出局:

numbers are[I@2e4b1dd8


推荐答案

输出完全正常。数组不会覆盖 toString()方法,因此它会调用 Object#toString() 方法,生成那种代表权。输出的格式如下:

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())

对于数组, Class#getName() method对不同的元素类型使用一些编码来生成唯一的类名。编码规则在文档中指定。

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.

要获得人类可读的表示,可以使用 数组#toString() 方法:

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

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

这篇关于打印阵列显示错误输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆