如何使用索引值调用和使用另一个类中的ArrayList中的项

时间:2015-09-12 01:56:59

标签: java arraylist indexing

在这个计划中,我将创建一个员工名单。为此,我必须创建多个ArrayLists,您将在我的代码中看到它们。我的第一堂课#34; EmployeeRoster"持有名字和姓氏,税号和小时工资。在这堂课中输入所有这些都很好,很花哨。但是,在我的" Employee_Payment" class我需要提示用户先输入员工的号码,或者输入ArrayList中的号码。然后,程序应该知道它需要引用哪个名字和姓氏,以便用户可以输入他们在一周内工作的周和小时。我在调用ArrayLists方面遇到了问题。我尝试过在网上找到的东西,但我得不到任何东西或空白数组。我希望能够输入一个索引值,以便用户或我可以输入某个员工工作的小时数。我希望我描述得足够清楚。谢谢!

EmployeeRoster类:

 import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * 
 */
public class EmployeeRoster {

    String firstName =" ";
    String lastName = " ";
    String tid =" ";
    double wage = 0; 
ArrayList<String> firstNames = new ArrayList<>();
ArrayList<String> lastNames = new ArrayList<>();
ArrayList<String> tax = new ArrayList<>();
 ArrayList<Double> hw = new ArrayList<>();



public void  Roster()
{
  Scanner n = new Scanner(System.in);  

  while(!firstName.equalsIgnoreCase("d"))
  {
    System.out.println("Enter employee record information."); 
    firstName = n.next();
    if(firstName.equalsIgnoreCase("d"))
    {
        break;
    }
    lastName = n.next();
    tid = n.next();
    wage=n.nextDouble();
    firstNames.add(firstName);
    lastNames.add(lastName);
    tax.add(tid);
    hw.add(wage);



  for( int i = 1; i <= firstNames.size(); i++)
  {
      System.out.println(String.format( "%03d %s" + " | " + tax.get(i-1) + " | " + hw.get(i-1), i," | "+ firstNames.get(i-1) + " " + lastNames.get(i-1))  ); 
  }
 }

}

   public ArrayList<Double> getList()
{

    return hw;
}
public ArrayList<String> getString()
{
    return firstNames;
}
}

Employee_Payment类:

import java.util.Scanner;
import java.util.ArrayList;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author 
 */
public class Employee_Payment {
     double hours = 0.0;
     int week = 0;
     int refer = 0;
     ArrayList<Double> hw = (new EmployeeRoster()).getList();
     ArrayList<String> firstNames = (new EmployeeRoster()).getString();


    public void payment()
    {

        System.out.println("Please enter employee ID number then week and hours worked.");
        Scanner h = new Scanner(System.in);
       refer = h.nextInt();
        //This was a first attempt and I know that it is really wrong
      for(int i = 0; i<hw.size(); i++)
      {

           if(refer == i)
         {
           System.out.println(firstNames.get(i));
         }
      }
    }
}

所以我希望能够像这样输入第二类中的模块:

     Number Week Hours
        2     1    40.0
        2     2    40.0

然后,数字2将引用数组中的第二个对象。

1 个答案:

答案 0 :(得分:1)

我认为您不需要所有这些ArrayList来实现您想要实现的目标。您可以使用Map的单个ArrayList或EmployeeRoster

现在您当前逻辑的问题是,当您创建一个新的Object时,您不会在EmployeeRoster类中初始化ArrarLists。相反,他们正在另一种方法Roster()进行初始化。你没有在你的代码中的任何地方打电话。

您可以在调用此方法的EmployeeRoster类中创建默认的无参数构造函数,或使用EmployeeRoster类的对象调用它。在这里,我将使用第二种方法。

您的逻辑的另一个问题是,在Employee_Payment类中,您正在使用两个不同的hw对象初始化firstNamesEmployeeRoster列表。您应该在Employee_Payment的构造函数中完成此操作。

public class Employee_Payment {
    double hours = 0.0;
    int week = 0;
    int refer = 0;
    ArrayList<Double> hw;
    ArrayList<String> firstNames;

    public Employee_Payment(){
        EmployeeRoster roster = new EmployeeRoster();
        roster.Roster();

        hw = roster.getList();
        firstNames = roster.getString()
    }

    //....Your rest of the logic
}

现在hwfirstNames列表将包含数据,您可以在pament()方法中使用它们。