这就是我到目前为止向下滚动到main并且你会看到//第7部分这是我必须做一个小循环包含搜索我的树的if语句的人,即在树集中以h开头的名字的人SP1
import java.util.*;
public class assignment8 {
public static void main(String[] args) {
//question1
System.out.println(months.MARCH.season());
System.out.println(months.SEPTEMBER.season());
System.out.println("------------------");
//question2
TreeSet<Person> sp1 = new TreeSet<Person>(); //a set of people(person is the object)
sp1.add(new Person("David"));
sp1.add(new Person("Sarah"));
sp1.add(new Person("BenDover"));
sp1.add(new Person("Jack"));
sp1.add(new Person("Mark"));
System.out.println(sp1);
TreeSet<Person> sp2 = new TreeSet<Person>(); //a set of people(person is the object)
sp2.add(new Person("David"));
sp2.add(new Person("Sarah"));
sp2.add(new Person("Jess"));
sp2.add(new Person("James"));
sp2.add(new Person("Adam"));
System.out.println(sp2);
//part1
System.out.println("Boolean: "+sp1.containsAll(sp2));
//part2
TreeSet<Person> insp = new TreeSet<Person>(sp1);
insp.retainAll(sp2);
System.out.println("Intersection: " +insp);
//part3
TreeSet<Person> unsp = new TreeSet<Person>(sp1);
unsp.addAll(sp2);
System.out.println("Union: " +unsp);
//part4
TreeSet<Person> diffsp = new TreeSet<Person>(sp1);
diffsp.removeAll(sp2);
System.out.println("Dfference in sp1 and sp2: " +diffsp);
//part5
TreeSet<Person> diffsp1 = new TreeSet<Person>(sp2);
diffsp1.removeAll(sp1);
System.out.println("Dfference in sp2 and sp1: " +diffsp1);
//part6
System.out.print("Are diffsp & diffsp1 the same: "+diffsp.equals(diffsp1));
//part7
for (Person current : sp1){
if(current.charAt(0)=='m' || current.charAt(0)=='M'){
System.out.println("Name begining with M:"+current.toString())
}
}
//Question 1
enum Season {
SPRING,SUMMER,AUTUMN,WINTER;
public String toString(){
switch(this){
case SPRING:
return "Spring";
case SUMMER:
return "Summer";
case AUTUMN:
return "Autumn";
default:
return "WINTER";
}
}
}
enum months {
JANUARY,FEBUARY,MARCH,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER;
public String toString(){
switch(this){
case JANUARY:
return "January";
case FEBUARY:
return "Febuary";
case MARCH:
return "March";
case APRIL:
return "April";
case MAY:
return "May";
case JUNE:
return "June";
case JULY:
return "July";
case AUGUST:
return "August";
case SEPTEMBER:
return "September";
case OCTOBER:
return "October";
case NOVEMBER:
return "November";
default:
return "December";
}
}
public Season season(){
int k = this.ordinal(); //gets a month and makes k == to the ordinal value of that month
if (k == 0 || k == 10 || k ==11 )
return Season.WINTER;
else if (k==7 || k==8 || k==9)
return Season.AUTUMN;
else if (k==4 || k==5 || k==6)
return Season.SUMMER;
else
return Season.SPRING;
}
}
final class Person implements Comparable<Person>{
private final String sName;
Person(String n){sName = n;}
public String name(){return sName;}
public String toString(){return sName;}
public boolean equals(Object ob){
if (!(ob instanceof Person))
return false;
Person p = (Person)ob;
return sName.equals(p.sName);
}
public int compareTo(Person p){
return sName.compareTo(p.sName);
}
public int hashCode(){
return sName.hashCode();
}
}
答案 0 :(得分:0)
由于这是一项任务,我不会提供整个代码,但可以给你一个开始
TreeSet<String> set = new TreeSet<>();
set.add("Hello");
set.add("World");
set.add("!");
// making an iterator for traversing each element in the set
Iterator<String> iterator = set.iterator();
// iterator will iterate till it has something to traverse
while(iterator.hasNext()){
System.out.println(iterator.next()); // printing the element
}
// creating a new String
String s = "hello";
// printing the 2nd character of the String instance
// Note : String is considered as 0 based index like array
// so it will print 'l'
System.out.println(s.charAt(2));
答案 1 :(得分:-2)
您可以尝试向Person类添加一个布尔字段(flag),指示sName
是否以h开头(可以在构造函数中完成检查),还可以添加一个返回布尔变量值的getter方法。
因此在foreach
循环中,您可以使用此getter方法来检查对象标志的状态。