如何一个int数组转换为字符串在Java的toString方法 [英] How to convert an int array to String with toString method in Java
问题描述
我使用的尝试使用的toString(INT [])
方法,但我想我做错了:
<一个href=\"http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html#toString%28int%5B%5D%29\">http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html#toString(int[])
我的code:
INT []数组=新INT [lnr.getLineNumber()+ 1];
INT I = 0;的System.out.println(与Array.toString());
输出是:
[I @ 23fc4bec
我也尝试过这样的印花,但是:
的System.out.println(新的String()的toString(阵列)。); // **对下一行的错误**
的toString()方法中的String类型是不适用的参数(INT [])
我把这个code出更大,更复杂的code,但如果需要,我可以添加它。但是,这应该给的一般信息。
我要寻找的输出,如Oracle的文档中:
再presentation字符串由数组的元素列表,包含在方括号([])中。相邻元素由字符,(逗号后面有一个空格)分隔。
块引用>解决方案您需要的是
Arrays.toString(INT [])
方法:进口java.util.Arrays中;INT []数组=新INT [lnr.getLineNumber()+ 1];
INT I = 0;..的System.out.println(Arrays.toString(阵列));有一个静态
Arrays.toString
为每个不同的原始Java类型的辅助方法;一对<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(int[])\"><$c$c>int[]$c$c>这说:公共静态字符串的ToString(INT []一)
返回一个字符串,重新指定数组的内容presentation。再presentation字符串由数组的元素列表,包含在方括号(
[]
)。相邻元素由字符分隔,
(逗号后跟一个空格)。这些元素通过将String.valueOf转换为字符串(INT)
。返回空
如果A
为null。
块引用>I am using trying to use the
toString(int[])
method, but I think I am doing it wrong:http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html#toString(int[])
My code:
int[] array = new int[lnr.getLineNumber() + 1]; int i = 0; System.out.println(array.toString());
The output is:
[I@23fc4bec
Also I tried printing like this, but:
System.out.println(new String().toString(array)); // **error on next line** The method toString() in the type String is not applicable for the arguments (int[])
I took this code out of bigger and more complex code, but I can add it if needed. But this should give general information.
I am looking for output, like in Oracle's documentation:
The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space).
解决方案What you want is the
Arrays.toString(int[])
method:import java.util.Arrays; int[] array = new int[lnr.getLineNumber() + 1]; int i = 0; .. System.out.println(Arrays.toString(array));
There is a static
Arrays.toString
helper method for every different primitive java type; the one forint[]
says this:public static String toString(int[] a)
Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets (
"[]"
). Adjacent elements are separated by the characters", "
(a comma followed by a space). Elements are converted to strings as byString.valueOf(int)
. Returns"null"
ifa
is null.
这篇关于如何一个int数组转换为字符串在Java的toString方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!