问题
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<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();
}
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:
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 }
回答2:
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<String> 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<String> words = Arrays.asList(paints);
回答3:
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<String> colors = List.of("Red", "Blue", "Yellow");
String result = colors.stream().collect(Collectors.joining(" , ", "{", "}"));
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
来源:https://stackoverflow.com/questions/64305106/how-to-print-list-of-string-inside-bracket