简体   繁体   English

使用for循环遍历数组x。 将x的元素作为String返回,其中每个元素由单个空格分隔

[英]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++) {
...
} 

As seen in this answer . 从这个答案中可以看出

You should use a for and StringJoiner (credits to @sm4) to help with that purpose 您应该使用forStringJoiner (贷记@ 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进行格式化,然后从执行该任务的方法中输出生成的StringBuildertoString

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.

相关问题 如何使用 for 循环搜索此数组列表。 并检查它是否包含元素 x - How to search through this list of arrays using a for loop. And check to see if it contains an element x 我对如何正确编码 while 循环感到困惑。 如何确保它遍历数组中的所有值 - I am confused on how to properly code the while loop. How to make sure it iterates through all the values in the array 遍历数组中的每个元素 - Loop through each element in array 制作一个 10x2 的二维数组,每个元素设置为字符串“x” - Make a two-dimensional 10x2 array, each element set to the string "x" 给定一个长度为 N 的数组,其中数组中的每个元素都有 X 种可能性,返回所有可能的数组 - Given an array of length N, where each element in the array has X possibilities, return all possible arrays 数组副本排除每个x元素 - Array copy exclude each x element 如果在循环中数组的某个元素上,请执行X动作 - If on a certain element of the array in a loop, do X action 循环遍历数组,每个元素一个 JUnit 测试 - Loop through array, each element a JUnit test 循环遍历数组太多次Java - Loop iterates through array too many times Java 计算元素数组的 MIN、MAX 的程序,其中每个元素都是 Map<String,Float> - Program to calculate MIN, MAX of array of elements where each element is of Map<String,Float>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM