有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中的树映射实现只返回最后一个元素

这是一个类,我想把它的对象放在树形图中

public class JobDefinition  {
    private static String jobDescription;
    private static String datasetName;
    private static String jobName;
    private static String responsiblePerson;
    public JobDefinition(String jobDesc, String dataSet, String jobName2, String person) {
        jobDescription=jobDesc;
        datasetName=dataSet;
        jobName=jobName2;
        responsiblePerson=person;
    }
    public  String getJobDescription() {
        return jobDescription;
    }

    public  String getDatasetName() {
        return datasetName;
    }

    public  String getJobName() {
        return jobName;
    }

    public  String getResponsiblePerson() {
        return responsiblePerson;
    }

}

在这里,我使用POI库从电子表格中获取值。TreeMap使用一个整数作为键,上面类的对象作为其值

for (int rowCount = rowStartIndex+1; rowCount < rowEndIndex; rowCount++) 
    {
        String jobDesc=spreadsheet.getRow(rowCount).getCell(0).toString();
        String dataSet=spreadsheet.getRow(rowCount).getCell(1).toString();
        String jobName=spreadsheet.getRow(rowCount).getCell(2).toString();
        String person =spreadsheet.getRow(rowCount).getCell(3).toString();
        if(!jobName.equals("N/A") && jobName!=""){
            validJobCount++;
            jobDefinitionInfo.put(validJobCount, new JobDefinition(jobDesc,dataSet,jobName,person));
            }
    }
    for(Map.Entry<Integer,JobDefinition> entry : jobDefinitionInfo.entrySet()) {
          System.out.println(entry.getKey()+"::"+entry.getValue().getJobDescription());
        }

在地图中设置所有值时。我重复了一遍。我得到了正确的键,但所有对应的值(这是JobDefinition类的一个对象)都是放置的最后一个值

输出::

1::Monthly UPDTMEND File
2::Monthly UPDTMEND File
3::Monthly UPDTMEND File
4::Monthly UPDTMEND File
5::Monthly UPDTMEND File
6::Monthly UPDTMEND File
7::Monthly UPDTMEND File
8::Monthly UPDTMEND File
9::Monthly UPDTMEND File
10::Monthly UPDTMEND File

预期产出

1::VRSFEND - TRANSACTION SWEEP
2::XCTLOAD 
3::CHEKDATE  - TO IDENTIFY BACKDATED TRANSACTIONS
4::EDITALIVE  
5::EDITB 
6::PRICE LOAD
7::ACCTSIM - run manually 
8::ACCTLIV - run manually by DVG                        
9::Check Sybase jobs
10::Monthly UPDTMEND File

我觉得执行有问题。请告诉我还需要添加什么才能使其正确运行


共 (1) 个答案

  1. # 1 楼答案

    您正在参数中使用static。这意味着每次运行它们都会被覆盖,这很好地解释了为什么总是得到最后一个元素

    您需要更改为:

    private String jobDescription;
    private String datasetName;
    private String jobName;
    private String responsiblePerson;