我在组织员工名单时遇到麻烦。我只需要根据他们的员工类型(前两个字母)来组织它们。每个对象都以员工代码开头,这是前两个字母。这就是我需要分离头寸类型的原因,但是由于某些原因我无法抓住它们。

这是我要创建对象并将其存储在数组中的文件。


  PW_1234,James,Bond,01/02 / 10,1,10 PW_1235,John,Brown,02/03 / 10,2,10.5
  PW_1236,霍华德,约翰逊,03/04 / 10,3,11
  PW_1237,Francis,Themule,04/05 / 11,4,10.75
  PW_1238,马修,刘易斯,05/06 / 11,1,12.75
  PW_1239,马克,比克斯顿,05/13 / 11,2,13
  PW_1242,萨拉,格洛弗,05/14 / 11,1,13.75 PW_1245,约翰,母鹿,05/15 / 11,4,10.5
  PW_1245,玛丽,母鹿,05/15 / 11,4,10.5
  TL_1248,Abel,英语,05/16 / 11,3,16.5,0.01,100,89
  TL_1251,Eustis,Clauser,05/17 / 11,2,16,0.02,100,9
  SU_1254,Henry,Hollowman,05/18 / 11,1,40000,0.01
  PW_1240,Luke,Sailor,01/22 / 12,3,14.5 PW_1243,Jane,Baker,01/23 / 12,2,14
  PW_1243,简,贝克,01/23 / 12,2,14
  TL_1246,David,Brief,01/24 / 12,1,14.75,0.01,100,57
  PW_1246,David,Doson,01/24 / 12,1,14.75
  TL_1249,贝克,安德森,01/25 / 12,4,11.5,0.01,100,100
  TL_1252,Frank,Donson,01/26 / 12,3,17.5,0.02,100,39
  SU_1255,Issac,Asimov,01/27 / 12,2,43000,0.02
  SU_1256,艾萨克,肖尔曼,01/28 / 12,3,39000,0.01
  SU_1257,艾萨克,罗伯茨,01/29 / 12,4,35500,0.01
  PW_1241,约翰,糖果,11/23 / 13,4,9.5 PW_1244,凯特,史密斯,11/24 / 13,3,15.5
  PW_1244,Kate,Handle,11/24 / 13,3,15.5
  TL_1247,Samual,Dempky,11/25 / 13,2,15,0.01,100,10
  TL_1250,Charley,Boman,11/26 / 13,1,15.75,0.01,100,50
  TL_1253,乔治,弗里兹曼,11/27 / 13,4,12.5,0.02,100,27


这是代码:

private String makeEmployeeList()
{
    String list = "";

    for(int i=0; i < employees.length; i++)
    {
        list += "\n"+employees[i].toString();
        if(employees[i]substring(0,2).equals("SU"))
        {
            list += "\n"+employees[i].toString();
        }
    }
    return list;
}


**此处是创建employees数组的方式:

private Employee[] employees;


**这里是所有内容的加载方式。

public void loadEmployeesFromFile(String fileName)
            {
               File inFile = new File(fileName);
               if(inFile.exists())  //  MAKE SURE FILE EXISTS
               {
                   try
                   {
                       BufferedReader inReader = new BufferedReader(new FileReader(inFile));
                      inReader.mark(32000);
                       String inLine = inReader.readLine();

                       //************************************
                       //  Counting rows to set array size
                       //************************************

                       int rowCount = 0;
                       while (inLine != null  && !inLine.equals(""))
                       {
                            rowCount++;
                           inLine = inReader.readLine();
                       }
                       inReader.reset();

                       //*******************
                       // re-reading data
                       //*******************
                       this.employees = new Employee[rowCount];

                       for(int rowIndex = 0;rowIndex < rowCount; rowIndex++)
                       {
                            inLine = inReader.readLine();
                            Scanner employeeScanner = new Scanner(inLine).useDelimiter(",");
                            String workerType = employeeScanner.next();
                            String firstName = employeeScanner.next();
                            String lastName = employeeScanner.next();
                            String hireDate = employeeScanner.next();
                            int shift = employeeScanner.nextInt();

                            if(workerType.substring(0,2).equals("PW"))
                            {
                                double pay = employeeScanner.nextDouble();
                                employees[rowIndex]= new ProductionWorker(workerType, firstName, lastName, hireDate, shift, pay);
                            }
                            else if(workerType.substring(0,2).equals("TL"))
                            {
                                double pay = employeeScanner.nextDouble();
                                double bonusRate = employeeScanner.nextDouble();
                                int reqHours = employeeScanner.nextInt();
                                int recHours = employeeScanner.nextInt();
                                employees[rowIndex]= new TeamLeader(workerType, firstName, lastName, hireDate, shift, pay, bonusRate, reqHours, recHours);
                            }
                            else if(workerType.substring(0,2).equals("SU"))
                            {
                                double salary = employeeScanner.nextDouble();
                                double bonusRate = employeeScanner.nextDouble();
                                employees[rowIndex]= new ShiftSupervisor(workerType, firstName, lastName, hireDate, shift, salary, bonusRate );
                            }
                       }
                       return;
                   }catch(IOException ioe)
                   {
                       System.err.print("\nTrouble reading employee file: "+fileName);
                   }
               }
               JOptionPane.showMessageDialog(null, "\nFile Name does not exist!\n Process terminating!");
               System.exit(0);
            }

最佳答案

private String makeEmployeeList(){
StringBuilder sbSU = null;

for(int i=0; i < employees.length; i++)
{
    sbSU = new StringBuilder();
    if(employees[i].substring(0,2).equals("SU"))
    {
        sbSU.append(employees[i].toString());
    }
}
return sbSU.toString();
}


首先,您在emplyees [i]替代之后错过了一个点
由于字符串是一个不可变的对象,因此建议您使用StringBuilder及其附加方法代替+ =。并使用其toString()方法将StringBuilder转换为String。您还需要覆盖Employees的toString方法。

要对数组中的员工进行排序,您需要实现Comparable或Comparator接口,以便数组知道对员工进行排序时要使用的条件,在这种情况下,是比较员工的类型

关于java - 组织对象JAVA的数组列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21843892/

10-10 14:04
查看更多