Java 数组打印出奇怪的数字和文本 [英] Java arrays printing out weird numbers and text

查看:37
本文介绍了Java 数组打印出奇怪的数字和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

好的,我马上去.

这是我的代码:

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

这可以编译并运行良好.这只是让我头晕目眩的 CMD 输出.

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

这是输出:[I@3e25a5.

我希望输出代表列表中完全相同的数字 (arr).我该如何实现?

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

推荐答案

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

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 int array. To print the data in the array, you can use:

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

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

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

这篇关于Java 数组打印出奇怪的数字和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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