【问题标题】:method wrong output [duplicate]方法错误输出[重复]
【发布时间】:2015-02-13 17:53:20
【问题描述】:

当我输入代码时,我会得到类似 [I@190d11.我该如何解决?

public static void main(String[] args) 
{
    int N;
    N = 10; 
    System.out.println(getRandom(N));           
}

public static int[] getRandom(int N)
{
    int i, j, x, y;
    Random generator = new Random();
    int[] random = new int[N * 2];
    x = (2 * N) - 1;
    y = -N;
    for(i = y; i <= N; i++)
    {
        for(j = 0; j < N - 1; j++)
        random[j] = generator.nextInt(x - y - 1) + y;
    }
    return random;
}

我认为它与静态有关,但我无法解决。

【问题讨论】:

  • 打印输出是正确的,因为您正在打印 int[] 如果您正在寻找实际值,则需要循环数组以获取值
  • @harold_finch 你期望的输出是什么
  • 它的打印数组。所以它只会喜欢那个。
  • 将输出行替换为 System.out.println(Arrays.toString(getRandom(N)));
  • 我想要一个大小为 19 的数组,所有数组都有一个介于 -10 和 10 之间的随机数。

标签: java arrays methods


【解决方案1】:

您正在打印 array ,其默认为 String 打印 objectName 和 hashCode。 如果要打印数组中的所有值,请使用:-

System.out.println(Arrays.toString(getRandom(N)));   

或者遍历数组并分别打印每个元素

【讨论】:

  • 谢谢
  • 欢迎您,您的问题实际上是重复的,但我回答它的唯一原因是您无法弄清楚您是从方法中获取数组。因此,可能会错过这样的细节。您能否通过单击答案旁边的勾号来排除此答案。
猜你喜欢
  • 2014-12-06
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 2015-09-13
  • 2014-08-26
  • 2021-10-05
  • 2013-08-17
  • 2018-11-28
相关资源
最近更新 更多