java小问题,谢谢

来源:百度知道 编辑:UC知道 时间:2024/05/12 00:54:44
import java.util.*;
public class Student implements Comparable<Student> {
String name;
Map<String,Integer> scores=new HashMap<String,Integer>();
public Student(String name){
this.name=name;
}
public void addScore(String scoreName,int value){
this.scores.put(scoreName,value);
}
public int getTotal(){
Collection<Integer> c=this.scores.values();
int total=0;
for(int i:c){
total+=i;
}
return total;
}

public int compareTo(Student s){
return s.getTotal()-this.getTotal();
}

public String toString(){
return name+" "+getTotal();
}
}
问题:for(int i:c)这种表达方法我看着不习惯,能不能帮我改成原始的表达方式

for(type identifier:iterable_expression){}这种请示一般用于集合,也可以用于数组.
identifier是用户指定类型的标识符
iterable_expression是指定了一个集合的表达式

在集合里,这是固定的模式,你如果想改的话,那么程序要做重新定义,这样的话,改动很大,而且没有意义.原来是你27行代码,重新改动后就会变成117行代码.
你只要知道意思的就可以了,因为这种用法在实际工作中应用不是很大

for(int i;i<c.size;i++){
total+=i;
}