Java 数组打印出奇怪的数字和文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4479683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:55:36  来源:igfitidea点击:

Java arrays printing out weird numbers and text

javaarraystextnumbers

提问by Racket

I'm new to programming. I'm sure the answer for this question is out there, but I have no idea what to search for.

我是编程新手。我确定这个问题的答案就在那里,但我不知道要搜索什么。

Ok, I'll go right to it.

好的,我马上去。

Here's my code:

这是我的代码:

int[] arr;
arr = new int[5];

arr[0] = 20;
arr[1] = 50;
arr[2] = 40;
arr[3] = 60;
arr[4] = 100;

System.out.println(arr);

This compiles and works fine. It's just the output from CMD that I'm dizzy about.

这编译并正常工作。这只是 CMD 的输出让我头晕目眩。

This is the output: [I@3e25a5.

这是输出:[I@3e25a5

I want the output to represent the exact same numbers from the list (arr) instead. How do I make that happen?

我希望输出代表列表 ( arr) 中完全相同的数字。我该如何做到这一点?

回答by Goran Jovic

It's the default string representation of array (the weird text).

它是数组的默认字符串表示(奇怪的文本)。

You'll just have to loop through it:

你只需要遍历它:

for(int i : arr){
System.out.println(i);
}

回答by duffymo

Like this:

像这样:

for (int i = 0; i < arr.length; ++i)
{
    System.out.println(arr[i]);
}

That "weird number" is the reference for the array you printed out. It's the default behavior built into the java.lang.Object toString() method.

这个“奇怪的数字”是您打印出的数组的参考。这是内置在 java.lang.Object toString() 方法中的默认行为。

You should override it in your own objects if seeing the reference isn't sufficient.

如果看到引用还不够,您应该在您自己的对象中覆盖它。

回答by Enrique

To print the values use.

要打印值,请使用。

for(int i=0; i<arr.length; i++)
   System.out.println(arr[i]);

回答by Hovercraft Full Of Eels

Every object has a toString()method, and the default method is to display the object's class name representation, then @followed by its hashcode. So what you're seeing is the default toString()representation of an intarray. To print the data in the array, you can use:

每个对象都有一个toString()方法,默​​认方法是显示对象的类名表示,然后@是其哈希码。所以你看到的是数组的默认toString()表示int。要打印数组中的数据,您可以使用:

System.out.println(java.util.Arrays.toString(arr));

Or, you can loop through the array with a forloop as others have posted in this thread.

或者,您可以for像其他人在此线程中发布的那样使用循环遍历数组。

回答by Emil

System.out.println(Arrays.toString(arr));

The current output is classtype@hashcode.

当前输出为classtype@hashcode

Incase you need to print arrays with more than one dimension use:

如果您需要打印多维数组,请使用:

Arrays.deepToString(arr);

Also remember to override toString()method for user-defined classes so that you get a representation of the objet as you choose and not the default represention which is classtype@hashcode

还要记住覆盖toString()用户定义类的方法,以便您在选择时获得对象的表示,而不是默认表示classtype@hashcode

回答by EnabrenTane

You printed the reference and not the values at the reference... One day it will all become clear with C.

您打印的是参考而不是参考中的值……总有一天,C 会变得清晰。

回答by Oswald

for (int i = 0; i < arr.length; ++i)
{
  System.out.println(arr[i]);
}

回答by user547215

It prints it's .toString() method you should print each element

它打印它的 .toString() 方法你应该打印每个元素

for(int i=0; i<arr.length; i++) {
   System.out.println(arr[i]);
}

or

for(Integer i : arr) {
  System.out.println(i);
}

回答by Peter Lawrey

BTW You can write

BTW 你可以写

int[] arr = { 20, 40, 60, 40, 60, 100 };
System.out.println(Arrays.toString(array));

or even

甚至

System.out.println(Arrays.toString(new int[] { 20, 40, 60, 40, 60, 100 }));

or

或者

System.out.println(Arrays.asList(20, 40, 60, 40, 60, 100));

回答by Anton Dozortsev

My version of a shorter!

我的版本更短!

Use Arrays.toString()and PrintStream.printf(String format, Object... args).

使用Arrays.toString()PrintStream.printf(String format, Object... args)

System.out.printf("%s%n", Arrays.toString(arr));