实在没看出来哪里有问题🤣
package zh.codegym.task.task08.task0819;
import java.util.HashSet;
import java.util.Set;
/*
包含猫的集
*/
public class Solution {
public static void main(String[] args) {
Set<Cat> cats = createCats();
//在此编写你的代码. 第 3 步
cats.remove(cats.toArray()[0]);
printCats(cats);
}
public static Set<Cat> createCats() {
//在此编写你的代码. 第 2 步
HashSet<Cat> cats = new HashSet<Cat>();
cats.add(new Cat("1"));
cats.add(new Cat("2"));
cats.add(new Cat("3"));
return cats;
}
public static void printCats(Set<Cat> cats) {
// 第 4 步
for(Cat cat : cats){
System.out.println(cat);
}
}
// 第 1 步
public static class Cat{
String catName="";
public Cat(){
}
public Cat(String name){
catName=name;
}
public String toString(){
return catName;
}
}
}
for (Cat cat : cats) {
you can not use to delete anything from the set But you could delete the element outside the loop or you use the iterator and as said, this may work as well but I do not know how CodeGym tests the code [this is for somewhen later: you could use streams to get a object from a collection] I'd use the second approach using an Iterator ;)