如何在括号内打印字符串列表?

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

How to print list of string inside bracket?

问题

以下是翻译好的内容:

我是Java的新手。我在尝试打印一个由逗号分隔的字符串列表,放在括号里。

public class House extends Property {
    protected static List<String> paints;
    public String paint;

    public House() {
        super("House", 45);
        String paints = new String();
        System.out.print(paints);
    }

    public void addPaint(String paint) {
        this.paint = paint;
        ArrayList<String> House = new ArrayList<String>();
        House.add(" ");
    }

    public void display() {
        super.display();
        List<String> words = Arrays.asList(paint);
        StringJoiner strJoin = new StringJoiner(", ", " { ", " }");
        words.forEach((s) -> strJoin.add(s));
        if (paint == null || "".equals(paint)) {
            System.out.print(" { }");
        } else {
            System.out.print(strJoin);
        }
    }

    public static void main(String[] args) {
        House house1 = new House();
        house1.addPaint("Red");
        house1.addPaint("Blue");
        house1.addPaint("Yellow");
        house1.display();
    }
}

对于有颜色的房子,应该打印如下:

45 House { Red, Blue, Yellow }

对于没有颜色的房子(空的),应该打印如下:

45 House { }

然而,我的代码的输出只打印了最后添加的颜色:

45 House { Yellow }

请帮助我。谢谢。

英文:

I am new to Java. I am facing a problem to print a list of string separated by commas inside brackets.

public class House extends Property { 
protected static List&lt;String&gt; paints;
public String paint;

public House() {
    super(&quot;House&quot;, 45);
    String paints = new String();
    System.out.print(paints);
}

public void addPaint(String paint) {
    this.paint = paint;
    ArrayList&lt;String&gt; House = new ArrayList&lt;String&gt;();
    House.add(&quot; &quot;);

public void display() {
    super.display();
    List&lt;String&gt; words = Arrays.asList(paint);
    StringJoiner strJoin = new StringJoiner(&quot;, &quot;, &quot; { &quot;, &quot; }&quot;);
    words.forEach((s)-&gt;strJoin.add(s));
    if (paint == null || &quot;&quot;.equals(paint)) {
        System.out.print(&quot; { }&quot;);
    }

    else {
    System.out.print(strJoin);
    }

public static void main(String[] args) {
        House house1 = new House();
        house1.addPaint(&quot;Red&quot;);
        house1.addPaint(&quot;Blue&quot;);
        house1.addPaint(&quot;Yellow&quot;);
        house1.display();
    }

It should be printed like this for a house with colors:

45 House { Red, Blue, Yellow }

Or like this for a house without color (empty):

45 House { }

However, the output of my code only prints the last added color:

45 House { Yellow }

Please help me. Thank you

答案1

得分: 1

使用前缀和后缀来对字符串列表进行分组,您可以使用Collectors.joining(CharSequence delimiter, CharSequence prefix,CharSequence suffix)),示例:

import java.util.List;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {
        List<String> colors = List.of("Red", "Blue", "Yellow");
        String result = colors.stream().collect(Collectors.joining(" , ", "{", "}"));
        System.out.println(result);
    }
}

这将输出{Red , Blue , Yellow},如果列表为空,将输出{}

请注意

  • 我认为您应该重新审查一下您的模型设计 ^^!
  • 在我的示例中,我使用了List.of()来演示“如何”,需要Java 9或更高版本。
英文:

To group a list of strings using a prefix and suffix you can use Collectors.joining(CharSequence delimiter, CharSequence prefix,CharSequence suffix)), example :

import java.util.List;
import java.util.stream.Collectors;

public class Test {
    public static void main(String[] args) {
        List&lt;String&gt; colors = List.of(&quot;Red&quot;, &quot;Blue&quot;, &quot;Yellow&quot;);
        String result = colors.stream().collect(Collectors.joining(&quot; , &quot;, &quot;{&quot;, &quot;}&quot;));
        System.out.println(result);
    }
}

this will print {Red , Blue , Yellow} and if list is empty it will print {}

Note that:

  • I think you should review your model design ^^ !
  • I've used List.of() in my example to demonstrate the "how to", knowing that it requires java 9 or above

答案2

得分: 0

你已经在这里将paint声明为一个字符串(而不是字符串列表)。

public String paint;

然后你将字符串变量转换为一个列表。

List<String> words = Arrays.asList(paint);

这就是为什么列表words只有一个字符串,它是通过house1.addPaint()添加的最后一个字符串,该方法只是在其中赋值字符串this.paint = paint;

有很多方法可以修复这个问题,但我会告诉你一种需要最少更改的方法。

我的建议:

public void addPaint(String paint) {
    paints.add(paint);
}

List<String> words = Arrays.asList(paints);
英文:

You have declared paint as a String (and not a List of Strings) here.

public String paint;

And then you convert the String variable to a List.

List&lt;String&gt; words = Arrays.asList(paint);

Which is why the list words has only the single String which was added last with house1.addPaint(), which just assigns the String this.paint = paint; inside it.

There are many ways you can fix it. But I'll tell the one that requires the least amount of changes.

My suggestion:

public void addPaint(String paint) {
    paints.add(paint);
}

and

List&lt;String&gt; words = Arrays.asList(paints);

</details>



# 答案3
**得分**: -3

```java
public class House {
    List<String> colors = new ArrayList<String>(); 
    public void addPaint(String color)
    {
        colors.add(color);        
    }
    public void display()
    {   String value = "";
        for (String color : colors)
	        value += color + ", ";
        value = value.substring(0, value.length()-2);
        System.out.println("{ " + value + " }");
    }
}

public static void main(String[] args) {
    House house1 = new House();
    house1.addPaint("Red");
    house1.addPaint("Blue");
    house1.addPaint("Yellow");
    house1.display();
}

// Output: { Red, Blue, Yellow }
英文:
public class House {
    List&lt;String&gt; colors = new ArrayList&lt;String&gt;(); 
    public void addPaint(String color)
    {
        colors.add(color);        
    }
    public void display()
    {   String value = &quot;&quot;;
        for (String color : colors)
	    value += color + &quot;, &quot;;
        value = value.substring(0, value.length()-2);
        System.out.println(&quot;{ &quot; + value + &quot; }&quot;);
    }
}

public static void main(String[] args) {
    House house1 = new House();
    house1.addPaint(&quot;Red&quot;);
    house1.addPaint(&quot;Blue&quot;);
    house1.addPaint(&quot;Yellow&quot;);
    house1.display();
}

// Output: { Red, Blue, Yellow }

huangapple
  • 本文由 发表于 2020年10月11日 22:26:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64305106.html
匿名

发表评论

匿名网友

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

确定