Java 如何在一行中打印数组的结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16564505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:26:40  来源:igfitidea点击:

How to print the results of an array in a single line?

java

提问by Cleiton Lima

I would like to know if anybody could help me to understand why my array result does not come in one single line. The results of the code below is printed as:

我想知道是否有人可以帮助我理解为什么我的数组结果没有出现在一行中。下面代码的结果打印为:

[
1
2
3
4
5
6
7
8
9
10
]

Instead of [1 2 3 4 5 6 7 8 9 10].

而不是[1 2 3 4 5 6 7 8 9 10].

Any thoughts on what I am doing wrong to the results not come in on line?

关于我做错了什么的任何想法都没有上线?

class RangeClass {

    int[] makeRange(int lower, int upper) {
      int arr[] = new int[ (upper - lower) + 1 ];

      for(int i = 0; i < arr.length; i++) {
        arr[i] = lower++;
      }
      return arr;
    }

    public static void main(String arguments[]) {
    int theArray[];
    RangeClass theRange = new RangeClass();

    theArray = theRange.makeRange(1, 10);
    System.out.println("The array: [ ");
    for(int i = 0; i< theArray.length; i++) {
      System.out.println(" " + theArray[i] + " ");
    }
    System.out.println("]");
    }
}   

回答by DeadlyJesus

Replace System.out.printlnby System.out.printlike this:

替换System.out.printlnSystem.out.print这样:

System.out.print("The array: [ ");
for(int i = 0; i< theArray.length; i++) {
  System.out.print(" " + theArray[i] + " ");
}
System.out.println("]");

printlnadd a line separator at the end of what you just printed.

println在刚刚打印的内容的末尾添加一个行分隔符。

回答by Juned Ahsan

Use System.out.print instead of System.out.println

使用 System.out.print 而不是 System.out.println

回答by Adam Stelmaszczyk

You can use shorter version:

您可以使用较短的版本:

int theArray[] = {1, 2, 3};
System.out.println(java.util.Arrays.toString(theArray));

回答by roger_that

System.out.printlnchanges the line after printing the statement. That is why, your array elements are not coming in one line. Try using System.out.printinstead and that would do the trick.

System.out.println在打印语句后更改行。这就是为什么你的数组元素不是在一行中。尝试使用System.out.print代替,这样就可以了。

回答by Azad

for(int i = 0 ; i < theArray.length;i++){
 if(i==0)
   System.out.print("["+theArray[i]);
 else if(i==theArray.length-1)
   System.out.print(","+theArray[i]+"]");
 else
   System.out.print(","+theArray[i]);
}

Output :(for example)

输出:(例如)

[1,2,5,3,7]

[1,2,5,3,7]

回答by Kunal Kishore

Use System.out.print() at the place of System.out.println() in all the code because if you use System.out.println() a new line character is getting printed after our output on console each time it is called but if you use System.out.print() it will print what you are passing it as parameter. So Change your code to

在所有代码中在 System.out.println() 的位置使用 System.out.print() 因为如果您使用 System.out.println() 每次我们在控制台上的输出之后都会打印一个换行符调用但如果您使用 System.out.print() 它将打印您作为参数传递的内容。所以将您的代码更改为

class RangeClass {
    int[] makeRange(int lower, int upper) {
        int arr[] = new int[ (upper - lower) + 1 ];
        for(int i = 0; i < arr.length; i++) {
            arr[i] = lower++;
        }
        return arr;
    }

    public static void main(String arguments[]) {
        int theArray[];
        RangeClass theRange = new RangeClass();

        theArray = theRange.makeRange(1, 10);
        System.out.print("The array: [ ");
        for(int i = 0; i< theArray.length; i++) {
            System.out.print(" " + theArray[i] + " ");
        }
        System.out.print("]");
    }
}

回答by Kangkan Lahkar

int []arr = {3, 4, 1, 7, 8, 5, 4, 11, 33, 21, 17, 15};

int []arr = {3, 4, 1, 7, 8, 5, 4, 11, 33, 21, 17, 15};

System.out.print(Arrays.toString(arr).replace("[","").replace("]", "").replace(",", ""));

System.out.print(Arrays.toString(arr).replace("[","").replace("]", "").replace(",", ""));

回答by Jad Chahine

You can do this in one line using Java 8

您可以使用Java 8在一行中完成此操作

Assume you have this list of integers

假设你有这个整数列表

List<Integer> lst = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8 ,9);

You can print all the items in one shot in this way

您可以通过这种方式一次性打印所有项目

lst.forEach(nb -> System.out.print(nb + " "));

Or in this way

或者以这种方式

lst.forEach(System.out::print);

Result

结果

1 2 3 4 5 6 7 8 9

回答by Pranav

Just make one small change to your code:

只需对您的代码进行一个小的更改:

class ArrayDemo {

  int[] makeRange(int lower, int upper) {
    int arr[] = new int[ (upper - lower) + 1 ];

    for(int i = 0; i < arr.length; i++) {
      arr[i] = lower++;
    }
    return arr;
  }

  public static void main(String arguments[]) {
    int theArray[];
    ArrayDemo theRange = new ArrayDemo();

    theArray = theRange.makeRange(1, 10);
    System.out.print("The array: [ ");  //Remove println here
    for(int i = 0; i< theArray.length; i++) {
      System.out.print(" " + theArray[i] + " "); //Same here
    }
    System.out.println("]");
  }
}

Output array: [ 1 2 3 4 5 6 7 8 9 10 ]

输出数组: [ 1 2 3 4 5 6 7 8 9 10 ]