0
我對Java很新,但我覺得這是一件容易的事。這個數組列表有兩個元素...名字和分數。我想寫一個方法來打印列表中所有名字的列表,而不是分數。我知道我笑ArrayList包含兩個元素,如何只返回String元素?
import java.util.ArrayList;
/**
* Print test scrose and student names as well as he average for the class.
*/
public class TestScores {
private ArrayList<Classroom> scores;
public int studentScores;
/**
* Create a new ArrayList of scores and add some scores
*/
public TestScores() {
scores = new ArrayList<Classroom>();
}
/**
* Add a new student and a new score.
*/
public void add (String name, int score) {
scores.add(new Classroom(name, score));
if(score > 100){
System.out.println("The score cannot be more than 100");
}
}
/**
* Return all the student names.
*/
public void printAllNames() {//this is the method.
for (Classroom s : scores){
System.out.println(scores.get(name));
}
}
}
和教室類是如何做到這一點,我只是不記得之前:
import java.util.ArrayList;
/**
* This class creates the names and scores of the students
*/
public class Classroom {
public int score;
public String name;
/**
* Constructor for the Class that adds a name and a score.
*/
public Classroom(String aName, int aScore) {
score = aScore;
name = aName;
}
/**
* Return the name of the students
*/
public String returnName() {
return name;
}
/**
* Return he scores
*/
public int returnScore() {
return score;
}
}
太謝謝你了。我完全明白! – feelingstoned
不客氣 – MartinS