从JavaFX中的ArrayList访问元素

时间:2016-03-02 05:26:03

标签: arraylist javafx

我使用的ArrayList位于返回ArrayList的方法中,但却无法识别ArrayList中的start。如何访问ArrayList与常规数组中的元素,是否正确地从方法返回ArrayList?谢谢!

    try
    {
        ArrayList<Integer> scores = loadFile("scores.txt");          
    }catch(FileNotFoundException e) 
    {
        System.err.println("File error: " + e.getMessage());
    }catch(IOException e) 
    {
        System.err.println("IO error: " + e.getMessage());
    }

    GridPane root = new GridPane();
    primaryStage.setScene(new Scene(root, 600, 600));

    //linePane();
    //textPane();

    Circle circle = new Circle();
    circle.setRadius(1.0f);
    for(int i = 0; i < 59; i++)
    {
        double polar = (double)(scores[i]);
private static ArrayList<Integer> loadFile(String filename) throws IOException 
{
    int numberOfRecords = 0;

    Scanner fin = new Scanner(new File(filename));

    ArrayList<Integer> scores = new ArrayList();

    while(fin.hasNext()) 
    {
        scores.add(fin.nextInt());
        numberOfRecords++;
    }

    Collections.sort(scores);

    return scores;
}

1 个答案:

答案 0 :(得分:1)

你从一个Arraylist获得的值不是得分[index],而是使用一个desginated方法。

scores.get(i);

-------------编辑----------------- 代码不能识别分数,因为它超出了范围。

ArrayList <Integer> scores = null;
try{
    scores = //code
} catch {}

if(scores != null){
    scores.get(i);
}

-------------------- EDIT ------------------- 第二个问题:折线。 Polyline的add方法采用Double而不是Integer(score是Integers的ArrayList)。因此,将其转换为Double。

 line.getPoints().add((double)scores.get(i));

您关于调用错误的其他问题。由于您在方法&#34; line&#34;中存在非法参数异常而引发它。基本上,您将相同的节点添加到根目录太多次了。

private static void line(){
    Polyline line = new Polyline();
    for(int i = 0; i < 59; i++)
     {
        line.getPoints().add((double)scores.get(i));

     }
    root.getChildren().add(line); // <- outside of the for loop
}