例子程序如下,这是测试程序,为了模拟项目里要做的
public class Score { private String name;
private Integer chinese;
private Integer math;
public static void main(String[] args) {
List<Score> scores = new ArrayList<Score>();
Score score1 = new Score();
score1.name="张三";
score1.chinese = 89;
score1.math = 90;
Score score2 = new Score();
score2.name="张三";
score2.chinese = 78;
score2.math = 83;
Score score3 = new Score();
score3.name="李四";
score3.chinese = 67;
score3.math = 50;
Score score4 = new Score();
score4.name="王五";
score4.chinese = 88;
score4.math = 99;
Score score5 = new Score();
score5.name="李四";
score5.chinese = 80;
score5.math = 86;
Score score6 = new Score();
score6.name="王五";
score6.chinese = 90;
score6.math = 96;
Score score7 = new Score();
score7.name="张三";
score7.chinese = 76;
score7.math = 90;
Score score8 = new Score();
score8.name="李四";
score8.chinese = 88;
score8.math = 83;
scores.add(score1);
scores.add(score2);
scores.add(score3);
scores.add(score4);
scores.add(score5);
scores.add(score6);
scores.add(score7);
scores.add(score8); for(int i=0;i<scores.size();i++){
for(int j=i+1;j<scores.size();j++){
Score s1 = scores.get(i);
Score s2 = scores.get(j);
if(s1.name.equals(s2.name)){
Integer chinese2 = s2.chinese;
Integer math2 = s2.math;
s1.chinese=s1.chinese+chinese2;
s1.math=s1.math+math2;
scores.remove(s2);
}
}
}
for(int i=0;i<scores.size();i++){
Score s = scores.get(i);
System.out.println(s.name+":语文="+s.chinese+" 数学="+s.math);
}
}}那个两层循环有时候结果是正确的,有时候不正确,那样写有问题,不知道有没有别的思路和解决办法?
这个例子程序想实现如下结果:张三:语文=243 数学=263
李四:语文=235 数学=219
王五:语文=178 数学=195
public class Score { private String name;
private Integer chinese;
private Integer math;
public static void main(String[] args) {
List<Score> scores = new ArrayList<Score>();
Score score1 = new Score();
score1.name="张三";
score1.chinese = 89;
score1.math = 90;
Score score2 = new Score();
score2.name="张三";
score2.chinese = 78;
score2.math = 83;
Score score3 = new Score();
score3.name="李四";
score3.chinese = 67;
score3.math = 50;
Score score4 = new Score();
score4.name="王五";
score4.chinese = 88;
score4.math = 99;
Score score5 = new Score();
score5.name="李四";
score5.chinese = 80;
score5.math = 86;
Score score6 = new Score();
score6.name="王五";
score6.chinese = 90;
score6.math = 96;
Score score7 = new Score();
score7.name="张三";
score7.chinese = 76;
score7.math = 90;
Score score8 = new Score();
score8.name="李四";
score8.chinese = 88;
score8.math = 83;
scores.add(score1);
scores.add(score2);
scores.add(score3);
scores.add(score4);
scores.add(score5);
scores.add(score6);
scores.add(score7);
scores.add(score8); for(int i=0;i<scores.size();i++){
for(int j=i+1;j<scores.size();j++){
Score s1 = scores.get(i);
Score s2 = scores.get(j);
if(s1.name.equals(s2.name)){
Integer chinese2 = s2.chinese;
Integer math2 = s2.math;
s1.chinese=s1.chinese+chinese2;
s1.math=s1.math+math2;
scores.remove(s2);
}
}
}
for(int i=0;i<scores.size();i++){
Score s = scores.get(i);
System.out.println(s.name+":语文="+s.chinese+" 数学="+s.math);
}
}}那个两层循环有时候结果是正确的,有时候不正确,那样写有问题,不知道有没有别的思路和解决办法?
这个例子程序想实现如下结果:张三:语文=243 数学=263
李四:语文=235 数学=219
王五:语文=178 数学=195
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;public class Score { private String name;
private Integer chinese;
private Integer math;
public static void main(String[] args) {
List<Score> scores = new ArrayList<Score>();
Score score1 = new Score();
score1.name="张三";
score1.chinese = 89;
score1.math = 90;
Score score2 = new Score();
score2.name="张三";
score2.chinese = 78;
score2.math = 83;
Score score3 = new Score();
score3.name="李四";
score3.chinese = 67;
score3.math = 50;
Score score4 = new Score();
score4.name="王五";
score4.chinese = 88;
score4.math = 99;
Score score5 = new Score();
score5.name="李四";
score5.chinese = 80;
score5.math = 86;
Score score6 = new Score();
score6.name="王五";
score6.chinese = 90;
score6.math = 96;
Score score7 = new Score();
score7.name="张三";
score7.chinese = 76;
score7.math = 90;
Score score8 = new Score();
score8.name="李四";
score8.chinese = 88;
score8.math = 83;
scores.add(score1);
scores.add(score2);
scores.add(score3);
scores.add(score4);
scores.add(score5);
scores.add(score6);
scores.add(score7);
scores.add(score8);
Map<String, Score> map = new HashMap<String, Score>();
for (Score score : scores) {
if(map.containsKey(score.name)){
score.chinese+=((Score)map.get(score.name)).chinese;
score.math+=((Score)map.get(score.name)).math;
map.put(score.name, score);
}else{
map.put(score.name, score);
}
}
for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
String name = (String) iterator.next();
System.out.println(name+"---"+map.get(name).chinese+"---"+map.get(name).math);
}
}}
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;/**
* 该类创建于 2009-7-3 上午10:52:59
*
* @version 1.0.0
* @author 侯磊
*/
public class Score { private String name;
private Integer chinese;
private Integer math; public static void main(String[] args) {
List<Score> scores = new ArrayList<Score>();
Score score1 = new Score();
score1.name = "张三";
score1.chinese = 89;
score1.math = 90; Score score2 = new Score();
score2.name = "张三";
score2.chinese = 78;
score2.math = 83; Score score3 = new Score();
score3.name = "李四";
score3.chinese = 67;
score3.math = 50; Score score4 = new Score();
score4.name = "王五";
score4.chinese = 88;
score4.math = 99; Score score5 = new Score();
score5.name = "李四";
score5.chinese = 80;
score5.math = 86; Score score6 = new Score();
score6.name = "王五";
score6.chinese = 90;
score6.math = 96; Score score7 = new Score();
score7.name = "张三";
score7.chinese = 76;
score7.math = 90; Score score8 = new Score();
score8.name = "李四";
score8.chinese = 88;
score8.math = 83; scores.add(score1);
scores.add(score2);
scores.add(score3);
scores.add(score4);
scores.add(score5);
scores.add(score6);
scores.add(score7);
scores.add(score8); Map<String, Score> total = doStatistic(scores);
printResults(total);
} private static void printResults(Map<String, Score> total) {
for (Iterator<Map.Entry<String, Score>> itr = total.entrySet().iterator();itr.hasNext();) {
Score s = itr.next().getValue();
System.out.println(s.name + ":语文=" + s.chinese + " 数学=" + s.math);
}
} private static Map<String, Score> doStatistic(List<Score> scores) {
Map<String, Score> total = new HashMap<String,Score>();
for(Iterator<Score> itr=scores.iterator();itr.hasNext();){
Score sc = itr.next();
Score tsc = total.get(sc.name);
if(tsc==null){
Score t = new Score();
t.name=sc.name;t.chinese=sc.chinese;t.math=sc.math;
total.put(sc.name, t);
}else{
tsc.chinese += sc.chinese;
tsc.math += sc.math;
}
}
return total;
}}
private Integer chinese;
private Integer math;
public static void main(String[] args) {
List<Score> scores = new ArrayList<Score>();
Score score1 = new Score();
score1.name="张三";
score1.chinese = 89;
score1.math = 90;
Score score2 = new Score();
score2.name="张三";
score2.chinese = 78;
score2.math = 83;
Score score3 = new Score();
score3.name="李四";
score3.chinese = 67;
score3.math = 50;
Score score4 = new Score();
score4.name="王五";
score4.chinese = 88;
score4.math = 99;
Score score5 = new Score();
score5.name="李四";
score5.chinese = 80;
score5.math = 86;
Score score6 = new Score();
score6.name="王五";
score6.chinese = 90;
score6.math = 96;
Score score7 = new Score();
score7.name="张三";
score7.chinese = 76;
score7.math = 90;
Score score8 = new Score();
score8.name="李四";
score8.chinese = 88;
score8.math = 83;
scores.add(score1);
scores.add(score2);
scores.add(score3);
scores.add(score4);
scores.add(score5);
scores.add(score6);
scores.add(score7);
scores.add(score8);
boolean[] isCounted=new boolean[scores.size()]; //加一个表示是否被统计过的标志数组。 for(int i=0;i<scores.size();i++){
Score s1=scores.get(i);
for(int j=i+1;j<scores.size();j++){
Score s2 = scores.get(j);
if(!isCounted[j]&&s1.name.equals(s2.name)){ //没有被统计过的才可以统计,都统计到同名人的第一个人的成绩上
//Integer chinese2 = s2.chinese;
//Integer math2 = s2.math;
s1.chinese+=s2.chinese;
s1.math+=s2.math;
isCounted[j]=true; //统计过之后,把相应参加统计的标志设为true
}
}
}
for(int i=0;i<scores.size();i++){
if(!isCounted[i]){ //把上面统计过程中,没有打统计标志的,也就是同名人中的第一个人的成绩输出
Score s = scores.get(i);
System.out.println(s.name+":语文="+s.chinese+" 数学="+s.math);
}
}
}}