2014-04-07 4 views
0

여기에 CreateEmployee 클래스가 있는데이 클래스를 Company 클래스 내부에서 사용하고 싶습니다. 내가 어떻게 그 일을하는거야?Java. ArrayList를 다른 클래스로 이식

내가 시도한 것은 회사 클래스 내에 직원 클래스 코드를 작성하는 것이 었습니다. 일 것이다, 그러나, 나는 다른 클래스는 ArrayList를

public class CreateEmployee { 

    private ArrayList<Employee> employeeList; 
    Scanner fileScanner; 

    String name; 
    String pw; 
    String [] dataArray; 
    String data; 
    int salary; 

    public CreateEmployee(){ 

     try 
     { 
      fileScanner = new Scanner(new File("employee.dat")); 
     } 
     catch (FileNotFoundException ex) 
     { 
      System.out.println("FileNotFoundException caught"); 
     } 

     employeeList = new ArrayList<Employee>(); 

     while(fileScanner.hasNextLine()) 
     { 
      //reset the array for every new line 
      dataArray = new String [10]; 

      //take the file line and split into 3 different parts 
      data = fileScanner.nextLine(); 
      dataArray = data.split("\\|"); 

      //assign the 3 different parts to the variables. 
      name = dataArray[0]; 
      pw = dataArray[1]; // this is a error. only after first run. 
      salary = Integer.parseInt(dataArray[2]); 

      //create a player object, add to playerList 
      Employee employee = new Employee(name, pw, salary); 
      employeeList.add(employee); 
     } 
    } 


    public ArrayList getEmployeeList(){ 
     return employeeList; 
    } 

    public void funcA(){ 
     //sample method to be executed at Company class 
    } 

} 


public class Company{ 
     public Company{ 
      //i would like to call the CreateEmployee constructor here. 
      // how should i go about it? 
     } 

     public static void main(String[] args){ 
       new Company().run(); 
     } 

     public void run(){ 
      //i would like to call a function of CreateEmployee class here. 
      // how should i go about it? 
     { 

} 
+0

왜 'CreateEmployee' 생성자에서 모든 작업을 수행하고 있습니까? '새로운 CreateEmployee();'? –

답변

0

당신이 필요합니다 곳은 경우에 도움이 될 것입니다, 더 많은 외부 클래스 및 회사의 클래스로 포트를에서 작업을 수행하는 방법에 대한 이해 싶습니다 다음과 같이 CreateEmployee 클래스의 새 인스턴스를 만듭니다.

CreateEmployee employees = new CreateEmployee(); 

그러면 getEmployeeList를 호출하고 결과를 저장합니다. 예 :

ArrayList<Employee> employeeList = employees.getEmployeeList(); 
관련 문제