2017-09-11 42 views
0

我正在研究一个项目,在这个项目中我将学习一个学生的选择并将它们添加到一个count数组(仍然在这部分中工作)。现在,我试图检索已发送并添加到Student类的Student ArrayList中的选项。在不同的类中返回并显示ArrayList的内容?

Student类:

public class Students { 

private String name; 
private ArrayList<Integer> choices = new ArrayList<Integer>(); 


public Students(){ 
    name = " "; 
} 

public Students(String Name){ 
    name = Name; 
} 

public void setName(String Name){ 
    name = Name; 
} 

public String getName(){ 
    return name; 
} 

public void addChoices(int Choices){ 
    choices.add(Choices); 
} 

public ArrayList<Integer> getChoices(){ 
    return choices; 
} 

这里是我的主要驱动器类:

public class P1Driver { 

public static void main(String[] args) throws IOException{ 

    ArrayList<Students> students = new ArrayList<Students>(); 
    String[] choices = new String[100]; 
    int[] count; 
    Scanner scan1 = new Scanner(new File("Choices.txt")); 
    Scanner scan2 = new Scanner(new File("EitherOr.csv")); 

    // Scan the first file. 
    int choicesIndex = 0; 
    while(scan1.hasNextLine()){ 
     String line = scan1.nextLine(); 
     choices[choicesIndex] = line; 
     choicesIndex++; 
    } 
    scan1.close(); 

    // Scan the second file. 
    int studentIndex = 0; 
    while(scan2.hasNextLine()){ 
     String line = scan2.nextLine(); 
     String [] splits = line.split(","); 

     students.add(new Students(splits[0])); 

     for(int i = 1; i < splits.length; i++){ 
      students.get(studentIndex).addChoices(Integer.parseInt(splits[i])); 
     } 
     studentIndex++; 
    } 
    scan2.close(); 

    // Instantiate and add to the count array. 
    int countIndex = 0; 
    for(int i = 0; i < students.size(); i++){ 
     if(students.get(i).getChoices(i) == -1){ 

     } 
    } 

最后一部分是现在的我。这远远没有做得很明显(我正好在它的中间),但在我建立一个for循环以从学生那里得到选择时,我得到一个错误,说:“The method getChoices()in the类型学生不适用于参数(int)。“有人可以解释这是什么意思,我错误在哪里,并可能如何解决它?谢谢大家。

+0

'getChoices()'不没有定义任何参数,但是你试着通过'i'。 – tima

+0

我看到了,我试图用i来标记ArrayList的索引。所以我应该添加一个索引来通过参数?像: public ArrayList getChoices(int index)?让我知道如果我仍然离开基地! –

回答

0

getChoices(int i)是不是你定义的方法。

if(students.get(i).getChoices(i) == -1){ 

} 

getChoices()返回一个列表,这样你就可以只使用get方法就行了:

if(students.get(i).getChoices().get(i) == -1){ 

} 

或者,做一个getChoice方法:

public Integer getChoice(int i){ 
    return choices.get(i); 
} 
0

你试过getChoices()[I],而不是getChoices(I)

+0

''''只适用于数组,不适用于'ArrayList';正确的用法是'getChoices()。get(i)'。 –

相关问题