如何从一个类调用和搜索arraylists到另一个类?

时间:2013-05-25 04:35:53

标签: java arraylist filereader

我想知道如何从一个班级到另一个班级调用和搜索arraylist。 这是我的第一堂课:

public void putIntoArray() {
        try
        {

        FileInputStream intoArrays = new FileInputStream("NRLdata.txt");
          // Get the object of DataInputStream
          DataInputStream in = new DataInputStream(intoArrays);
          BufferedReader arr = new BufferedReader(new InputStreamReader(in));
          Scanner src = new Scanner(intoArrays);
         ArrayList<Integer> year = new ArrayList<Integer>();
          ArrayList<String> winners = new ArrayList<String>();
          ArrayList<String> losers = new ArrayList<String>();
          ArrayList<String> premiers = new ArrayList<String>();
          ArrayList<String> spooners = new ArrayList<String>();
          ArrayList<Boolean> finals = new ArrayList<Boolean>();
          ArrayList<Integer> winningScore = new ArrayList<Integer>();
          ArrayList<Integer> losingScore = new ArrayList<Integer>();
          ArrayList<Integer> attendance = new ArrayList<Integer>();

          src.useDelimiter(",");

          while (src.hasNext()) {
                year.add(src.nextInt());
                winners.add(src.next());
                losers.add(src.next());
                premiers.add(src.next());
                spooners.add(src.next());
                finals.add(src.nextBoolean());
                winningScore.add(src.nextInt());
                losingScore.add(src.nextInt());
                attendance.add(src.nextInt());
            }
            Integer[] years = new Integer[year.size()];
            year.toArray(years);
            String[] winner = new String[winners.size()];
            winners.toArray(winner);
            String[] loser = new String[losers.size()];
            losers.toArray(loser);
            String[] premier = new String[premiers.size()];
            premiers.toArray(premier);
            String[] spoons = new String[spooners.size()];
            spooners.toArray(spoons);
            Boolean[] held = new Boolean[finals.size()];
            finals.toArray(held);
            Integer[] winScore = new Integer[winningScore.size()];
            winningScore.toArray(winScore);
            Integer[] loseScore = new Integer[losingScore.size()];
            losingScore.toArray(loseScore);
            Integer[] crowds = new Integer[attendance.size()];
            attendance.toArray(crowds);
          src.close();
          arr.close();
          }catch (Exception e){    //Catch exception if any
          System.err.println("Error: " + e.getMessage());
          }

}

她的是我的另一堂课:

import java.io.*;
import java.util.*;

public class GrandFinal {


public void readFromFile() {

    Scanner search = new Scanner(System.in);
    int grandYear;
    System.out.println("What grand Final would you like to search for");
    grandYear = search.nextInt();


}
   }

如您所见,我想根据用户输入的年份搜索特定的grandfinal。

1 个答案:

答案 0 :(得分:0)

您将在第一个类中返回arrayList,然后将列表作为参数传递给新函数:

public void readFromFile(yourArrayList) {
...

您不会从文本文件中读取它,因为您没有将其写入文本文件。

此外,您将需要添加搜索arrayList对象并返回所需对象的逻辑。

希望有所帮助。