数组未按预期初始化

huangapple go评论96阅读模式
英文:

Array not initializing as predicted

问题

以下是翻译好的部分:

我是一个刚开始学习Java的初学者。我最近阅读了以下内容:

通过new分配的数组中的元素将自动初始化为零(对于数值类型),false(对于布尔类型)或null(对于引用类型)
参考链接:https://www.geeksforgeeks.org/arrays-in-java/

int n = scan.nextInt();
int a[] = new int[n];
int a1[] = new int[5];
System.out.println(a);
System.out.println(a1);

这两个数组都给我返回了一个类似"[I@4b67cf4d"的乱码值。

为什么会发生这种情况?

英文:

I am a beginner to Java. I read this recently

The elements in the array allocated by new will automatically be initialized to zero (for numeric types), false (for boolean), or null (for reference types)
reference https://www.geeksforgeeks.org/arrays-in-java/

int n=scan.nextInt();
int a[]=new int[n];
int a1[]=new int[5];
System.out.println(a);
System.out.println(a1);

Both arrays are giving me a garbled value something like [I@4b67cf4d

Why is this happening?

答案1

得分: 1

你需要定义ToString()来获取对象(如数组)的漂亮字符串输出。数组默认情况下没有定义ToString()方法。

相反,尝试使用

Arrays.toString(a)
英文:

You need to define ToString() to get a nice string print out for objects like Arrays. Arrays don't have one defined by default.

Instead, try using

Arrays.toString(a)

答案2

得分: 0

你正在打印数组本身的字符串表示,而不是它们的内容。那种特定的表示方式编码了数据类型和存储地址。

实现你想要的一个方法是使用 java.util.Arrays.toString()

System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(a1));
英文:

You are printing the string representations of the arrays themselves, not of their contents. That particular representation encodes data type and storage address.

One way to get what you're after would be to use java.util.Arrays.toString():

System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(a1));

答案3

得分: 0

如果您希望所有的值以空格分隔,您可以使用for循环打印出这些值;虽然这样会更慢,但您可以根据自己的喜好进行更改。

        int n = scan.nextInt();
		int a[] = new int[n];
		int a1[] = new int[5];

		// 打印 a
		System.out.println("Array a 的值:");
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
		System.out.println(); // 换行

		// 打印 a1
        System.out.println("Array a1 的值:");
		for (int i = 0; i < a1.length; i++) {
			System.out.print(a1[i] + " ");
		}
		System.out.println();

在您的代码上表现很不错继续保持不断进步
英文:

If you want all the values separated by spaces, you can use a for loop to print out the values; this is slower but you can change it to your liking.

    int n = scan.nextInt();
	int a[] = new int[n];
	int a1[] = new int[5];

	// print a
	System.out.println(&quot;Array a values:&quot;);
	for (int i = 0; i &lt; a.length; i++) {
		System.out.print(a[i] + &quot; &quot;);
	}
	System.out.println(); // new line

	// print a1
    System.out.println(&quot;Array a1 values:&quot;);
	for (int i = 0; i &lt; a1.length; i++) {
		System.out.print(a1[i] + &quot; &quot;);
	}
	System.out.println();

Good job on your code, keep getting better!

huangapple
  • 本文由 发表于 2020年4月8日 05:11:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61089483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定