[英]Iterates through the array x using a for loop. Returns the elements of x as a String where each element is separated by a single space
public static String toStringFor(){
int[] x={3, 45, 17, 2, -1, 44, 9, 23, 67, 2, -6, -23, -100, 12, 5, 1212};
String array = x.toString();
return array;
}
I keep getting 我不断
[I@62aba879 [I @ 62aba879
What you are seeing is the hashcode of the array instance. 您所看到的是数组实例的哈希码。 You can't simply use toString()
on an array and expect to give you a nice result. 您不能简单地在数组上使用toString()
并期望给您一个不错的结果。 In fact, each object has its own toString()
method that can be implemented in various ways. 实际上,每个对象都有其自己的toString()
方法,可以通过各种方式来实现。 This is how the implementation of toString()
on an array looks like: 这就是在数组上toString()
的实现的样子:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
You will have to iterate through the elements and create the final string yourself. 您将不得不遍历元素并自己创建最终的字符串。 Here's a way to do it in Java 8: 这是在Java 8中执行此操作的一种方法:
int[] x={3, 45, 17, 2, -1, 44, 9, 23, 67, 2, -6, -23, -100, 12, 5, 1212};
String result = Arrays.stream(x)
.mapToObj(String::valueOf)
.collect(Collectors.joining(" "));
System.out.println("result = " + result);
This creates a stream of values from your array and collects them into a String separated with space. 这将从数组中创建值流,并将它们收集到以空格分隔的String中。
Another version with a for loop: 带有for循环的另一个版本:
StringJoiner sj = new StringJoiner(" ");
for (int i : x) {
sj.add(String.valueOf(i));
}
System.out.println("result =" + sj.toString());
You can also iterate the array using: 您还可以使用以下方法迭代数组:
for (int i = 0; i < x.length; i++) {
...
}
You should use a for and StringJoiner (credits to @sm4) to help with that purpose 您应该使用for和StringJoiner (贷记@ sm4)来帮助实现此目的
public static String toStringFor(){
int[] x={3, 45, 17, 2, -1, 44, 9, 23, 67, 2, -6, -23, -100, 12, 5, 1212};
StringJoiner sj=new StringJoiner(" ");
for (int i = 0; i < x.length; i++) {
sj.add(String.valueOf(x[i]));
}
return sj.toString();
}
toString
returns a string holding some abbreviation characters followed by the hexadecimal representation of a hash, unless overridden. toString
返回一个字符串,其中包含一些缩写字符,后跟哈希的十六进制表示,除非被覆盖。 Arrays don't override this, so you got the toString
of the object that the array is. 数组不会覆盖它,因此您获得了数组所在对象的toString
。
You need to write custom code to accomplish your goal. 您需要编写自定义代码来实现您的目标。 Perhaps you could loop through the array, appending the String
representation of each array element to a StringBuilder
along with some characters for formatting, and output the toString
of the resulting StringBuilder
from your method that performs that task. 也许您可以遍历数组,将每个数组元素的String
表示形式与一些字符一起附加到StringBuilder
进行格式化,然后从执行该任务的方法中输出生成的StringBuilder
的toString
。
The problem here is that you're calling toString()
on the array object - not on the elements within the array. 这里的问题是您要在数组对象上调用toString()
,而不是在数组中的元素上调用。
Use Arrays.toString(int[] a)
instead. 请改用Arrays.toString(int[] a)
。
In order to generate a string representation of the array, which is separated by space, it helps, if you handle the first array item independently and then add all further elements prefixed by a space. 为了生成由空格分隔的数组的字符串表示形式,如果您独立处理第一个数组项,然后添加所有以空格为前缀的元素,则将很有帮助。
int[] x = {1, 2, 3, 4};
int i = 0;
String arr = String.valueOf(x[i++]);
for (; i < x.length; i++)
arr += " " + x[i];
Doing so leaves no trailing space at the end of the string. 这样做在字符串的末尾不留尾随空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.