2013-05-25 1 views
-3

나는 한 클래스에서 다른 클래스로 arraylist를 호출하고 검색하는 방법에 대해 궁금해하고있었습니다. 다음은 내 첫 번째 클래스의 :한 클래스에서 다른 클래스로 arraylists를 호출하고 검색하는 방법은 무엇입니까?

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 검색 할 볼 수 있듯이.

+0

이 사이트는 디버깅 또는 코드 검토 서비스가 아닙니다. – Bohemian

답변

0

그런 다음 매개 변수로 새로운 기능으로 목록을 통과, 첫 번째 클래스의 arrayList을 반환 : 당신이 작성하지 않았기 때문에,

public void readFromFile(yourArrayList) { 
... 

당신은 텍스트 파일을 읽을 수없는 것 그것은 텍스트 파일에.

또한 arrayList 개체를 검색하고 원하는 개체를 반환하는 논리를 추가해야합니다.

희망이 있습니다.