如何将未选中的项目从列表移动到另一个? JList的

时间:2015-10-21 18:13:00

标签: java jlist

我试图仅将未选中的项目从列表移动到另一个列表中。此外,我想在移动它们之前验证这些项目是否已经在下一个列表中。

这是我到目前为止所拥有的

 int sel[] = lstNum1.getSelectedIndices();

    for (int i = 0; i < model1.getSize(); i++) {
        if(!model1.getElementAt(i).equals(model1.getElementAt(sel[i]).toString())){
            model2.addElement(model1.getElementAt(i).toString());
        }
    }

我试图比较位置上的项目&#34;我&#34;使用selectedArray中的项目,但没有运气。

2 个答案:

答案 0 :(得分:1)

AbtPst提供的答案的简单版本:

    Set<Integer> keepThese = new HashSet<Integer>();
    for (int x : sel) {
      keepThese.add(x);
    }

    for (int i=0 ; i<firstList.size() ; i++) {
       if( !keepThese.contains(i)) {
         if( !secondList.contains(firstList.get(i))) {
           secondList.add(firstList.get(i));
         }
       }
    }

答案 1 :(得分:0)

将你的int [] sel转换为Set。这样可以在添加到新列表之前轻松检查

  var data = new Bloodhound({
        datumTokenizer: function (datum) {
            return Bloodhound.tokenizers.whitespace(datum.value);
        },
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: "api/lookup/posearch/%QUERY",//,
            prepare: function (query, settings) {
                $("#loadingType").addClass('fa fa-circle-o-notch fa-spin');
                settings.url = "api/lookup/posearch/" + query;
                return settings;
            },               

            filter: function (m) {
                $("#loadingType").removeClass('fa fa-circle-o-notch fa-spin');

                // Map the remote source JSON array to a JavaScript object array
                return $.map(m, function (d) {

                    return {
                        value: d
                    };
                });
            }
        }
    });

    // Initialize the Bloodhound suggestion engine
    data.initialize();
    return data;

现在secondList将包含firstList中不在sel数组中的索引

的所有元素
相关问题