我想使用 for 循环整齐地打印一个一维数组。(Java)

huangapple go评论94阅读模式
英文:

I want to print a 1-dimensional array using a for loop neatly. (java)

问题

我正试图打印一个一维数组,并以清晰标记的输出形式呈现出来。我有一个不带参数的方法,用于打印这个一维数组,到目前为止,我已经创建了一个循环,它从id[0]到id[n-1]打印了数组的内容。我想要完成的主要任务是在我的主方法中,我调用显示一个数组的方法 3 次。输出需要清楚地定义每次调用的结果。例如:部分 A 的结果:(43,45,43,4,5,)。我应该使用 printf,或者我应该如何解决这个问题?我已经尝试在这个网站上查找了一段时间,但是没有找到足够具体的内容来帮助我。

到目前为止,我有:

  1. public void displaySubsets(){
  2. for(int i : array) {
  3. System.out.print("(");
  4. System.out.print(array[i]);
  5. System.out.print(")");
  6. }
英文:

I am trying to print a 1-dimensional array in a clearly labeled output presentation. I have a method that takes no arguments to print the one-dimensional array, and so far I've created a for loop that prints the contents of the array from id[0] to id[n-1]. The main task I want to accomplish is in my main method I call the method that displays an array 3 times. The output needs to clearly define the results for each call. Ex: Results for part A : (43,45,43,4,5,). Should I use printf or how should I approach the problem. Tried looking on this site for a while, but couldn't find anything specific enough to help.

So far I have

  1. public void displaySubsets(){
  2. for(int i : array) {
  3. System.out.print("(");
  4. System.out.print(array[i]);
  5. System.out.print(")");

}

答案1

得分: 2

你只需执行以下操作:

  1. System.out.println(Arrays.toString(array));

或者如果你想自己编写代码(假设数组不为空或为null):

  1. System.out.print("(" + array[0]);
  2. for (int i = 1; i < array.length; i++) {
  3. System.out.print(", " + array[i]);
  4. }
  5. System.out.println(")");
英文:

You can just do the following:

  1. System.out.println(Arrays.toString(array));

or if you want to roll your own. (assumes the array is not empty or null).

  1. System.out.print(&quot;(&quot; + array[0]);
  2. for (int i = 1; i &lt; array.length; i++) {
  3. System.out.print(&quot;, &quot; + array[i]);
  4. }
  5. System.out.println(&quot;)&quot;);
  6. </details>
  7. # 答案2
  8. **得分**: 0
  9. ## 以下是一种实现方法:
  10. 遍历数组并将数字拼接到新的字符串变量中。
  11. ```java
  12. static void printArray(){
  13. String printFormat = "";
  14. System.out.println("数组长度: " + array.length);
  15. //遍历数组
  16. for (int i=0; i < array.length; i++) {
  17. if (i+1 == array.length){
  18. //如果是数组中的最后一个值,我们不想添加", "
  19. //将数组的值拼接到新的字符串中,不带", "
  20. printFormat = printFormat + array[i];
  21. }else{
  22. //将数组的值拼接到新的字符串中
  23. printFormat = printFormat + array[i] + ", ";
  24. }
  25. }
  26. System.out.println("部分 A 的结果 : (" + printFormat + ")");
  27. }

点击这里运行代码

英文:

Here is one way of doing it:

Iterate over the array an concatenating the numbers into a new string variable.

  1. static void printArray(){
  2. String printFormat = &quot;&quot;;
  3. System.out.println(&quot;Array Length: &quot; + array.length);\
  4. //iterate over the array
  5. for (int i=0; i &lt; array.length; i++) {
  6. if (i+1 == array.length){
  7. //If is the last value in the array we don&#39;t want to add the &quot;, &quot;
  8. //concatenate the array&#39;s value into a new string without &quot;, &quot;
  9. printFormat = printFormat + array[i];
  10. }else{
  11. //concatenate the arrays value into a new string
  12. printFormat = printFormat + array[i] +&quot;, &quot;;
  13. }
  14. }
  15. System.out.println(&quot;Results for part A : (&quot; + printFormat + &quot;)&quot;);
  16. }

Click here to run the code

答案3

得分: 0

有不止一种方法可以做到:

我会选择我们的朋友“Java 8 Streams”,具体来说是Arrays类的stream()方法。它使代码更加优雅,直观且高效。

  1. package com.example;
  2. import java.util.Arrays;
  3. public class Main {
  4. public static void main(String[] args) {
  5. int arr[] = { 43, 45, 43, 4, 5 };
  6. System.out.print("(");
  7. Arrays.stream(arr)
  8. .forEach( e -> System.out.print(e + ",") );
  9. System.out.println(")");
  10. }
  11. }

> 输出:(43,45,43,4,5,)

英文:

There's more than 1 way to do it:

I would opt for our friend "Java 8 Streams", more specifically the stream() method of Arrays class. It makes it elegant, intuitive and efficient.

  1. package com.example;
  2. import java.util.Arrays;
  3. public class Main {
  4. public static void main(String[] args) {
  5. int arr[] = { 43, 45, 43, 4, 5 };
  6. System.out.print(&quot;(&quot;);
  7. Arrays.stream(arr)
  8. .forEach( e -&gt; System.out.print(e + &quot;,&quot;) );
  9. System.out.println(&quot;)&quot;);
  10. }
  11. }

> Output: (43,45,43,4,5,)

答案4

得分: 0

有很多方法可以解决这个问题。

替代方案1

你可以这样做:

  1. System.out.println(Arrays.toString(array));

就像其他人提到的那样。

替代方案2

  1. public static void main(String[] args) {
  2. Main main = new Main();
  3. int array[] = {43, 45, 43, 4, 5};
  4. System.out.println(main.printArray(array));
  5. }
  6. public String printArray(int[] array) {
  7. StringBuilder builder = new StringBuilder();
  8. builder.append("(");
  9. for (int i : array) {
  10. builder.append(i);
  11. builder.append(", ");
  12. }
  13. builder.append(")");
  14. return builder.toString();
  15. }

在打印方面,我喜欢使用 StringBuilder,因为它比直接使用 System.out.println() 更快。

希望对你有用!

英文:

There are many ways you can solve this.

Alternative 1

One way you can do this is just do:

  1. System.out.println(Arrays.toString(array));

Like other mentioned.

Alternative 2

  1. public static void main(String[] args) {
  2. Main main = new Main();
  3. int array[] = {43,45,43,4,5};
  4. System.out.println(main.printArray(array));
  5. }
  6. public String printArray(int[] array){
  7. StringBuilder builder = new StringBuilder();
  8. builder.append(&quot;(&quot;);
  9. for(int i : array){
  10. builder.append(i);
  11. builder.append(&quot;, &quot;);
  12. }
  13. builder.append(&quot;)&quot;);
  14. return builder.toString();
  15. }

I like to use StringBuilder when it comes to printing because it's faster than just doing System.out.println().

Hope it was useful!

huangapple
  • 本文由 发表于 2020年8月31日 07:42:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63663129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定