2

1 つのString配列と 1 つのList<String>. 私がやりたいことは、より大きなサイズの変数を使用し、それをより小さな変数の値の削除の基礎として使用することです。また、他の変数には存在しない、より大きなサイズの変数の値も取得したいと考えています。2 つの変数のデータ型が異なる理由は、String[] group変数が JSP ページのチェックボックス グループでありList<String> existingGroup、データベースの ResultSet であるためです。例えば:

String[] group内容:

Apple
Banana
Juice
Beef

List<String> existingGroup内容:

Apple
Beef
Lasagna
Flower
Lychee

また、2 つの変数のサイズが異なるため、値を正しく削除する必要があります。

私がこれまでに持っているのは

    if(groupId.length >= existingGroup.size()) {
        for(int i = 0; i < groupId.length; i++) {
            if(! existingGroup.contains(groupId[i])) {
                if(existingGroup.get(existingGroup.indexOf(groupId[i])) != null) {
                    // I'm unsure if I'm doing this right
                }
            }
        }
    } else {
        for(int i = 0; i < existingGroup.size(); i++) {
            // ??
        }
    }

ありがとう。

4

2 に答える 2

4

わかりました、配列をListあまりにも変換することから始めます。そうする

List<String> input = Arrays.asList(array);
//now you can do intersections
input.retainAll(existingGroup); //only common elements were left in input

または、一般的ではない要素が必要な場合は、

existingGroup.removeAll(input); //only elements which were not in input left
input.removeAll(existingGroup); //only elements which were not in existingGroup left

選択はあなた次第です:-)

于 2013-10-09T11:00:30.677 に答える
3

インターフェイスが提供するメソッドを使用できListます。

list.removeAll(Arrays.asList(array)); // Differences removed

また

list.retainAll(Arrays.asList(array)); // Same elements retained

あなたのニーズに基づいて。

于 2013-10-09T10:59:27.827 に答える