简体   繁体   English

Java中的列表字符串列表

[英]List of List String in java

I have a problem with the implementation of List>, it gives me always the last element duplicated. 我对List>的实现有问题,它总是使我最后一个重复的元素。 Here is a bunch of my code : 这是我的一堆代码:

Here is the declaration of my list and the list of lists. 这是我的名单的声明和名单的清单。

public List<List<String>> survs = new ArrayList<>();
public List<String> surveillance = new ArrayList<>();
public int k=0;

Here i add to the list some strings. 在这里,我将一些字符串添加到列表中。

  public void handleAddSURVClick(ActionEvent actionEvent) {
    survName = name.getText();
    max = maxp.getText();
    min = minp.getText();
    surveillance.add(survName);
    surveillance.add(monitoredObject);
    surveillance.add(monitoredProperty);
    surveillance.add(max);
    surveillance.add(min);
    // Add surveillance to the vector
    survs.add(k, surveillance);k++;
    //Surv initialisation
    survName = ""; name.clear();
    max = ""; maxp.clear();
    min = ""; minp.clear();
    monitoredObject = "";
    monitoredProperty = "";

}

then simply i add the list to the list of lists and i specify an index in which i wanna store my list and i print the list of lists ( survs ) 然后只需将列表添加到列表列表中,然后指定要在其中存储列表并打印列表列表的索引(survs)

    survs.add(k, surveillance);k++;
    System.out.println(survs);

Unfortunately, it gives me this result after submitting two lists, it gives just the last one redundant: 不幸的是,在提交两个列表之后,它给了我这个结果,它仅给出了最后一个冗余:

    [[yas, ProductSurrounding, charge, 667, 524, stack, ProductSurrounding, charge, 8787, 6422], [yas, ProductSurrounding, charge, 667, 524, stack, ProductSurrounding, charge, 8787, 6422]]

if i do surveillance.clear(), the results will be two empty lists [[],[]] 如果我执行监视.clear(),结果将是两个空列表[[],[]]

Thank you in advance 先感谢您

Create new instance of surveillance after each time you add it to the survs .But crate survs this instance only once. 创建新实例surveillance每次你把它添加到时间后survs 。但箱子survs这种情况下只有一次。 Do like this : 这样做:

    public void handleAddSURVClick(ActionEvent actionEvent) {
        survName = name.getText();
        max = maxp.getText();
        min = minp.getText();
        surveillance=new ArrayList<>(); 
        surveillance.add(survName);
        surveillance.add(monitoredObject);
        surveillance.add(monitoredProperty);
        surveillance.add(max);
        surveillance.add(min);
        // Add surveillance to the vector
        survs.add(k, surveillance);k++;
        //Surv initialisation
        survName = ""; name.clear();
        max = ""; maxp.clear();
        min = ""; minp.clear();
        monitoredObject = "";
        monitoredProperty = "";

    }

And if you do surveillance.clear() then you are clearing the value in the reference of surveillance in your survs .So at the end all becomes empty. 如果您执行了survs surveillance.clear()那么您将清除survssurveillance的引用中的值。因此,最后所有内容都为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM