for (String userName : studentEnrollments.keySet()) {
System.out.println("Student: " + userName);
ArrayList<String> courses = studentEnrollments.get(userName);
String courseMessage = "Courses: ";
for (String singleCourse : courses) {
courseMessage += singleCourse + ", ";
System.out.println(courseMessage);
}
}
在for循环之后,它会打印出学生正在学习的每门课程。在没有最后一个逗号的情况下用逗号打印字符串的正确方法是什么?
例如:AC130,AC140,AC150,AC130,AC140,AC150
答案 0 :(得分:6)
使用Iterator而不是for循环使用while,然后仅当hasNext()返回true时才添加昏迷。
String courseMessage = "Courses: ";
Iterator<String> c = studentEnrollments.get(userName).iterator();
while (c.hasNext()) {
courseMessage += c.next();
if (c.hasNext()) {
courseMessage += ",";
}
}
答案 1 :(得分:2)
请注意,在Java 8中,您可以使用StringJoiner
:
String commaSeparatedCourses = courses.stream().collect(Collectors.joining(", "));
答案 2 :(得分:1)
答案 3 :(得分:1)
这是另一种方法:
此方法可以解决此问题:除了在最后一个元素之外的每个元素之后添加逗号,不如在第一个元素之外的每个元素之前添加逗号。
这是一个实现
String separator = "";
for (String singleCourse : courses) {
courseMessage += separator + singleCourse;
System.out.println(courseMessage);
separator = ",";
}
答案 4 :(得分:1)
如果您使用的是java 8
,则可以使用StringJoinerStringJoiner joiner = new StringJoiner(", ");
for (String singleCourse : courses) {
joiner.add(singleCourse);
}
System.out.println(joiner.toString());
否则你只需在字符串前添加,
而不是附加它。
String courseMessage = "Courses: " + courses.get(0);
for (int i = 1; i<courses.size();++i) {
courseMessage += ", " + courses.get(i);
}
答案 5 :(得分:0)
使用StringBuilder:
for (String userName : studentEnrollments.keySet()) {
System.out.println("Student: " + userName);
ArrayList<String> courses = studentEnrollments.get(userName);
StringBuilder sb = new StringBuilder();
for (String singleCourse : courses) {
if(sb.length()>0){
sb.append(", ");
}
sb.append(singleCourse);
}
System.out.println(sb.insert(0, "Courses: ").toString());
}
答案 6 :(得分:0)
使用StringBuilder
for (String userName : studentEnrollments.keySet()) {
System.out.println("Student: " + userName);
ArrayList<String> courses = studentEnrollments.get(userName);
StringBuilder courseMessage = "Courses: ";
for (String singleCourse : courses) {
sb.append(singleCourse).append(", ");
}
if (sb.length() > 2)
sb.setLength(sd.length() -2);
String courseMessage = sb.toString();
System.out.println(courseMessage);
}
甚至更好地使用StringUtils(来自apache-common)
for (String userName : studentEnrollments.keySet()) {
System.out.println("Student: " + userName);
ArrayList<String> courses = studentEnrollments.get(userName);
String courseMessage = sb.toString();
System.out.println(StringUtils.join(courses, ", ");
}
答案 7 :(得分:0)
对于Java 8之前的解决方案,您还可以查看此代码段作为起点
String[] courses ={ "AC130", "AC140", "AC150" };
for (int i = 0; i < courses.length - 1; i++) {
System.out.append(courses[i]).append(", ");
}
System.out.println(courses[courses.length-1]);
小解释:对于所有元素,但最后一个元素打印“元素”+“,”,对于最后一个元素,它只打印“元素”。您需要确保自己不会处理空数组。
答案 8 :(得分:0)
使用string.replaceAll(", $", "")
,如下所示:
for (String userName : studentEnrollments.keySet()) {
System.out.println("Student: " + userName);
ArrayList<String> courses = studentEnrollments.get(userName);
String courseMessage = "Courses: ";
for (String singleCourse : courses) {
courseMessage += singleCourse + ", ";
}
System.out.println(courseMessage.replaceAll(", $", ""));
}