我的文本文件中没有重复项,但是当我打印出该组的大小时,它明显少于文本文件中的实际元素数....任何关于我如何解决这个问题的想法
这是主要方法
import java.util.*;
import java.util.Collection;
import java.io.*;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Schedule
{
public static void main (String args[])throws IOException
{
Scanner sc=new Scanner(new File("Footballteams.txt"));
ArrayList<Football>y=new ArrayList<Football>();
for(int i=0;i<114;i++)
{
String team=sc.nextLine();
Integer points=0;
String[]a=team.split("\\|",9);
String name=a[0];
int wins=Integer.parseInt(a[1]);
points+=wins;
int finalRecord14=Integer.parseInt(a[2]);
int finalRecord13=Integer.parseInt(a[3]);
int finalRecord12=Integer.parseInt(a[4]);
int finalRecord11=Integer.parseInt(a[5]);
int bowlVictories=Integer.parseInt(a[6]);
points=points+(bowlVictories*10);
int bowlLosses=Integer.parseInt(a[7]);
points=points-(bowlLosses*5);
int ConferenceChamp=Integer.parseInt(a[8]);
points=points+(ConferenceChamp*10);
Football x=new Football(name,wins,finalRecord14,finalRecord13,finalRecord12,finalRecord11,bowlVictories,bowlLosses,ConferenceChamp,points);
teams.add(x);
y.add(x);
}
System.out.println(teams.size());
System.out.println(y.size());
这是足球课程中的compareTo方法
public int compareTo(Football o)
{
return getPoints().compareTo(o.getPoints());
}'
这是我正在阅读的文件(我无法格式化它,但实际上每行一队)
Georgia Southern(Sun Belt)|9|-1|-1|-1|-1|0|0|1
Louisiana-Lafayette(Sun Belt)|36|0|0|0|0|4|0|1
Appalachian State(Sun Belt)|7|0|0|0|0|0|0|0
Texas State(Sun Belt)|17|0|0|0|0|0|0|0
Arkansas State(Sun Belt)|35|0|0|0|0|2|2|3
South Alabama(Sun Belt)|14|0|0|0|0|0|1|0
Louisiana-Monroe(Sun Belt)|22|0|0|0|0|0|1|0
Troy(Sun Belt)|17|0|0|0|0|0|0|0
New Mexico State(Sun Belt)|9|0|0|0|0|0|0|0
Idaho(Sun Belt)|5|0|0|0|0|0|0|0
Georgia State(Sun Belt)|5|0|0|0|0|0|0|0
Memphis(AAC)|19|0|0|0|25|1|0|1
Cincinnati(AAC)|38|25|0|0|0|2|2|3
Central Florida(AAC)|36|0|0|10|0|2|1|2
East Carolina(AAC)|31|0|0|0|0|1|2|0
Houston(AAC)|34|18|0|0|0|2|1|0
Temple(AAC)|21|0|0|0|0|1|0|0
South Florida(AAC)|14|0|0|0|0|0|0|0
Tulane(AAC)|14|0|0|0|0|0|1|0
Tulsa(AAC)|24|0|0|0|0|1|1|1
SMU(AAC)|21|0|0|0|0|2|0|0
Connecticut(AAC)|15|0|0|0|0|0|0|0
Florida State(ACC)|48|23|10|1|5|3|1|3
Clemson(ACC)|42|22|11|8|15|3|1|1
Louisville(ACC)|39|0|13|15|24|2|2|1
Boston College(ACC)|20|0|0|0|0|0|2|0
NC State(ACC)|26|0|0|0|0|2|1|0
Syracuse(ACC)|23|0|0|0|0|2|0|1
Wake Forest(ACC)|18|0|0|0|0|0|1|0
Georgia Tech(ACC)|33|0|0|0|8|2|2|0
Duke(ACC)|28|0|0|23|0|0|3|0
North Carolina(ACC)|28|0|0|0|0|1|2|0
Pittsburgh(ACC)|25|0|0|0|0|1|3|0
Miami(ACC)|28|0|0|0|0|0|2|0
Virginia Tech(ACC)|33|21|0|0|0|2|2|0
Virginia(ACC)|19|0|0|0|0|0|1|0
Baylor(XII)|40|13|0|13|7|2|2|2
TCU(XII)|44|14|0|0|3|2|1|2
Kansas State(XII)|38|15|12|0|18|0|3|1
Oklahoma(XII)|39|16|15|6|0|2|2|1
Texas(XII)|31|0|19|0|0|2|2|0
West Virginia(XII)|28|17|0|0|0|1|2|1
Oklahoma State(XII)|37|3|0|17|0|3|1|1
Texas Tech(XII)|25|0|0|0|0|2|0|0
Kansas(XII)|9|0|0|0|0|0|0|0
Iowa State(XII)|17|0|0|0|0|0|2|0
Ohio State(B1G)|44|0|3|12|1|2|2|1
Michigan State(B1G)|42|11|0|3|5|4|0|1
Maryland(B1G)|20|0|0|0|0|0|2|0
Rutgers(B1G)|32|0|0|0|0|2|2|0
Michigan(B1G)|31|12|24|0|0|1|2|0
Penn State(B1G)|31|0|0|0|0|1|1|0
Indiana(B1G)|14|0|0|0|0|0|0|0
Wisconsin(B1G)|39|10|0|22|13|1|3|2
Minnesota(B1G)|25|0|0|0|0|0|3|0
Nebraska(B1G)|37|24|25|0|0|1|3|0
Iowa(B1G)|26|0|0|0|0|0|3|0
Illinois(B1G)|19|0|0|0|0|1|1|0
Northwestern(B1G)|26|0|17|0|0|1|1|0
Purdue(B1G)|17|0|0|0|0|1|1|0
Army Black Knights(IND)|12|0|0|0|0|0|0|0
BYU Cougars(IND|34|0|0|0|0|2|2|1
Notre Dame Fighting Irish(IND)|39|0|4|20|0|1|2|3
Navy Midshipmen(IND)|30|0|0|0|0|2|1|0
Marshall(C-USA)|35|0|0|0|3|3|1|1
Middle Tennessee(C-USA)|26|0|0|0|0|0|2|0
Western Kentucky(C-USA)|30|0|0|0|0|1|1|0
Old Dominion(C-USA)|6|0|0|0|0|0|0|0
FIU(C-USA)|16|0|0|0|0|0|1|0
Florida Atlantic(C-USA|13|0|0|0|0|0|0|0
Louisiana Tech(C-USA)|30|0|0|0|0|0|0|0
Rice(C-USA)|28|0|0|0|0|2|1|1
UTEP(C-USA)|17|0|0|0|0|0|1|0
UTSA(C-USA)|19|0|0|0|0|0|0|0
UNT(C-USA)|22|0|0|0|0|1|0|0
Southern Miss(C-USA)|16|6|0|0|0|1|0|1
Fresno State(MWC)|30|0|0|0|0|0|3|1
Hawaii(MWC)|14|0|0|0|0|0|0|0
Nevada(MWC)|25|0|0|0|0|0|3|0
San Diego(MWC)|32|0|0|0|0|1|3|0
San Jose State(MWC)|25|0|21|0|0|1|0|0
UNLV(MWC)|13|0|0|0|0|0|1|0
Air Force(MWC)|25|0|0|0|0|1|2|1
Boise State(MWC)|43|6|14|0|16|3|1|1
Colorado State(MWC)|25|0|0|0|0|1|1|0
New Mexico(MWC)|12|0|0|0|0|0|0|0
Utah State(MWC)|37|0|17|0|21|3|1|1
Wyoming(MWC)|21|0|0|0|0|0|1|0
Alabama(SEC)|48|1|1|8|4|2|2|2
Arkansa(SEC)|10|5|0|0|0|2|0|0
Auburn(SEC)|31|0|0|2|23|1|2|1
LSU(SEC)|41|1|9|14|22|2|2|1
Ole Miss(SEC)|26|0|0|0|17|2|1|0
Mississippi State(SEC)|32|0|0|0|11|2|2|0
Texas A&M(SEC)|35|0|5|18|0|4|0|0
Arizona(Pac South)|30|0|0|19|2|2|1|0
UCLA(Pac South)|35|0|0|16|10|2|2|0
Arizona State(Pac South)|34|0|0|21|12|2|2|0
USC(Pac South)|36|6|0|19|20|2|1|0
Utah(Pac South)|27|0|16|0|21|2|0|0
Colorado(Pac South)|10|0|0|0|0|0|0|0
Oregon(PAC North)|48|2|9|2|4|1|1|2
Stanford(PAC North)|42|8|0|10|6|7|1|0
Washington(PAC North)|31|8|0|0|0|0|0|1
California(PAC North)|16|0|0|0|0|0|0|0
Washington State(PAC North)|16|0|0|0|0|0|0|0
Oregon State(PAC North)|24|0|0|19|0|0|0|0
Florida(SEC)|29|0|9|0|0|3|1|0
Georgia(SEC)|40|18|4|24|9|2|2|0
Kentucky(SEC)|14|0|0|0|0|0|0|0
Missouri Tigers(SEC)|35|0|0|5|14|2|0|0
South Carolina(SEC)|40|9|8|4|0|4|0|0
Tennessee(SEC)|22|0|0|0|0|1|0|0
Vanderbilt(SEC)|27|0|23|24|0|2|1|0
这是足球课
public class Football implements Comparable<Football>
{
public String name;
public int wins,finalRecord14,finalRecord13,finalRecord12,finalRecord11,bowlVictories,bowlLosses,ConferenceChamps;
public Integer points;
public Football(String name,int wins,int finalRecord14,int finalRecord13,int finalRecord12,int finalRecord11,int bowlVictories,int bowlLosses,int ConferenceChamp,Integer points)
{
this.name=name;
this.wins=wins;
this.finalRecord14=finalRecord14;
this.finalRecord13=finalRecord13;
this.finalRecord12=finalRecord12;
this.finalRecord11=finalRecord11;
this.bowlVictories=bowlVictories;
this.bowlLosses=bowlLosses;
this.ConferenceChamps=ConferenceChamp;
this.points=points;
}
public String getName()
{
return name;
}
public int getWins()
{
return wins;
}
public int getfinalRecord14()
{
return finalRecord14;
}
public int getfinalRecord13()
{
return finalRecord13;
}
public int getfinalRecord12()
{
return finalRecord12;
}
public int getfinalRecord11()
{
return finalRecord11;
}
public int getBowlVictories()
{
return bowlVictories;
}
public int getBowlLosses()
{
return bowlLosses;
}
public int getConferenceChamps()
{
return ConferenceChamps;
}
public Integer getPoints()
{
return points;
}
public int compareTo(Football o)
{
return getPoints().compareTo(o.getPoints());
}
}
答案 0 :(得分:2)
就java中的HashSet
而言,如果调用object1.equals(object2)
,并且该方法返回true,则这两个对象是相同的。如果这两个对象之间有一百万个不同的字段并不重要,重要的是如何实现equals
方法。我们已经知道Set
s不能有重复的元素。
如果您使用的是TreeSet
,那么您还必须实施与compareTo
方法一致的equals
方法,其中compareTo
返回0
时对象实际上是完全相同的。
Java不会为你做任何魔术,例如散列两个对象中的所有字节,或者序列化它们并比较它们(并非所有对象都是可序列化的!),或类似的东西。由您来决定何时两个对象实际相同的标准。
由于你需要在你的套装中拥有独特的足球队,你的equals
方法应该包括队名等的比较,而不仅仅是分数,因为多个队可能有相同的分数。