如何比较保存在ArrayList中的对象中包含的字符串,然后将这些字符串的值相加(如果它们彼此相等)?
我了解如何检查StringX == StringY
或StringX.equals(StringY)
,但我不明白如何比较ArrayList
中的字符串,更不用说如何将字符串的double值相加了对象(仅当该字符串相等时)。
示例 BaseBallTeam类具有两个属性;
我想将所有Kats(字符串)的所有homeRuns(double)加起来。以及所有mikes等制作的所有homeRuns(double)。
这里是我的代码:
ArrayList<BaseBallTeam> list = new ArrayList<>();
double totalRuns = 0;
for(int i = 0; i<list.size();i++){
for(int j = 1; j<list.size();j++){
if(list.get(i).getTeamMember().equals(list.get(j).getTeamMember())){
totalRuns += list.get(j).getHomeRuns() +list.get(i).getHomeRuns();
System.out.println("Amount of " + list.get(i).getTeamMember() + "s hit " + totalRuns"home runs);
}
}
我的代码为所有团队成员提供了累积频率,而不是将两个单独的分数相加
答案 0 :(得分:0)
您应该首先查看团队成员的人数,然后计算他们的本垒打:
ArrayList<BaseBallTeam> list = new ArrayList<>();
// fill the list
ArrayList<String> teamMembers = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (!teamMembers.contains(list.get(i).getTeamMember())) {
teamMembers.add(list.get(i).getTeamMember());
}
}
double totalRuns = 0;
for (int i = 0; i < teamMembers.size(); i++) {
double runs = 0;
for (int j = 0; j < list.size(); j++) {
if (list.get(j).getTeamMember().equals(teamMembers.get(i))) {
runs += list.get(j).getHomeRuns();
}
}
System.out.println("Amount of " + teamMembers.get(i) + "s hit " + runs + " home runs");
totalRuns += runs;
}
System.out.println("Total homeruns " + totalRuns);
答案 1 :(得分:0)
上一个答案很好用。这是简化代码并提高可读性的想法(尽管它做同样的事情!):
BaseBallTeam kate1 = new BaseBallTeam("Kate", 2.0);
BaseBallTeam kate2 = new BaseBallTeam("Kate", 3.0);
BaseBallTeam mike1 = new BaseBallTeam("Mike", 1.0);
BaseBallTeam mike2 = new BaseBallTeam("Mike", 5.0);
List<BaseBallTeam> list = Arrays.asList(kate1, kate2, mike1, mike2);
ArrayList<String> teamMembers = new ArrayList<>();
for(BaseBallTeam team : list){
if(!teamMembers.contains(team.getTeamMember())){
teamMembers.add(team.getTeamMember());
}
}
double totalRuns = 0;
for(String teamMember : teamMembers){
double runs = 0;
for(BaseBallTeam team : list){
if(team.getTeamMember().equals(teamMember)){
runs += team.getHomeRuns();
}
}
System.out.println("Amount of " + teamMember + "s hit " + runs + " home runs");
totalRuns += runs;
}
System.out.println("Total homeruns " + totalRuns);
答案 2 :(得分:0)
解决问题的最佳方法是使用Java 8功能。您可以按名称对所有BaseBallTeam对象进行分组,并使用java.util.stream.Collectors中的Collector对每个播放器的本垒打总数进行总计:
示例代码:
BaseBallTeam kate1 = new BaseBallTeam("Kate", 2);
BaseBallTeam kate2 = new BaseBallTeam("Kate", 2);
BaseBallTeam mike1 = new BaseBallTeam("Mike", 4);
BaseBallTeam mike2 = new BaseBallTeam("Mike", 2);
List<BaseBallTeam> list = Arrays.asList(kate1, kate2, mike1, mike2);
Map<String, Integer> map = list.stream()
.collect(Collectors.toMap(t -> t.getTeamMember(),
k -> k.getHomeRuns(),
(t1, t2) -> t1 + t2)
);
System.out.println(map);
输出:{Mike = 6,Kate = 4}
这里:
Collectors.toMap说您想将所有团队收集到HashMap
t-> t.getTeamMember()表示由其teamMember(name)收集。 teamMember成为HashMap的关键
k-> k.getHomeRuns()表示将其HomeRun作为值收集
(t1,t2)-> t1 + t2表示如果发现重复的键,我们希望对值求和
然后您可以按以下方式遍历地图:
map.entrySet().forEach(e ->
System.out.println("Amount of " + e.getKey() + "s hit " + e.getValue() + " home runs"));
输出:
迈克斯的命中率达到了6次本垒打
凯特(Kates)的命中次数达到4次本垒打