Return ArrayList 的值
Return value of ArrayList
我正在做一个 Java 练习,它告诉我编写一个 toString 方法,但我坚持使用 ArrayList 的 return 值。更多说明,请阅读toString部分的注释。
import java.util.ArrayList;
import java.util.Arrays;
public class Student {
/** The student's name */
private String name;
/** Course codes which the student is enrolled in */
private ArrayList<String> courses;
/** Creates a new student with the given name.
* @name the student's name
*/
public Student(String name) {
this.name = name;
this.courses = new ArrayList<String>();
}
/** Enrol in a course.
* @param course course code of the course being enrolled in. e.g. CSSE2002
*/
public void addCourse(String course) {
this.courses.add(course);
}
/**
* Returns the human-readable string representation of this student.
* The format of the string to return is
* "'name': courses='courseCodes'" without the single quotes,
* where 'name' is this student's name and 'courseCodes' is a comma-separated
* list of this student's enrolled courses.
* For example, if the student is enrolled several courses:
* "John Smith: courses=CSSE2002,DECO3801".
* If the student is enrolled in one course: "John Smith: courses=CSSE2002".
* If the student is not enrolled in any courses: "John Smith: courses=NO_COURSES".
* @return string representation of this student
*/
public String toString() {
Boolean CheckArray = courses.isEmpty();
if(CheckArray == true){
return this.name + ":" + " courses=NO_COURSES";
}else{
return this.name + ":" + " courses=" + this.courses;
}
}
}
当 运行 程序
时,我的测试用例一直出现此错误
我不知道如何乘坐额外的 [],这似乎是一个愚蠢的问题,如果有人能提供帮助,那就太好了。谢谢!
一种可能的解决方案是通过用空字符串替换括号来格式化 this.courses
的输出。
return this.name + ":" + " courses=" + Arrays.toString(this.courses).replace("[", "").replace("]", "");
根据代码注释,测试用例似乎期望输出如下:
John Smith: courses=CSSE2002,DECO3801
您可以使用 String.join
将课程代码与分隔符 ,
:
连接起来
return this.name + ":" + " courses=" + String.join(",", this.courses);
我正在做一个 Java 练习,它告诉我编写一个 toString 方法,但我坚持使用 ArrayList 的 return 值。更多说明,请阅读toString部分的注释。
import java.util.ArrayList;
import java.util.Arrays;
public class Student {
/** The student's name */
private String name;
/** Course codes which the student is enrolled in */
private ArrayList<String> courses;
/** Creates a new student with the given name.
* @name the student's name
*/
public Student(String name) {
this.name = name;
this.courses = new ArrayList<String>();
}
/** Enrol in a course.
* @param course course code of the course being enrolled in. e.g. CSSE2002
*/
public void addCourse(String course) {
this.courses.add(course);
}
/**
* Returns the human-readable string representation of this student.
* The format of the string to return is
* "'name': courses='courseCodes'" without the single quotes,
* where 'name' is this student's name and 'courseCodes' is a comma-separated
* list of this student's enrolled courses.
* For example, if the student is enrolled several courses:
* "John Smith: courses=CSSE2002,DECO3801".
* If the student is enrolled in one course: "John Smith: courses=CSSE2002".
* If the student is not enrolled in any courses: "John Smith: courses=NO_COURSES".
* @return string representation of this student
*/
public String toString() {
Boolean CheckArray = courses.isEmpty();
if(CheckArray == true){
return this.name + ":" + " courses=NO_COURSES";
}else{
return this.name + ":" + " courses=" + this.courses;
}
}
}
当 运行 程序
我不知道如何乘坐额外的 [],这似乎是一个愚蠢的问题,如果有人能提供帮助,那就太好了。谢谢!
一种可能的解决方案是通过用空字符串替换括号来格式化 this.courses
的输出。
return this.name + ":" + " courses=" + Arrays.toString(this.courses).replace("[", "").replace("]", "");
根据代码注释,测试用例似乎期望输出如下:
John Smith: courses=CSSE2002,DECO3801
您可以使用 String.join
将课程代码与分隔符 ,
:
return this.name + ":" + " courses=" + String.join(",", this.courses);