我想知道如何从一个类调用和搜索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();
}
}
如您所见,我想根据用户输入的年份搜索特定的总决赛。