-2

値を含む配列リストがあります

ArrayList<String> HexValues = new ArrayList<String>();

    HexValues.add("a");
    HexValues.add("b");
    HexValues.add("f");
    HexValues.add("1");
    HexValues.add("4");
    HexValues.add("0");
    HexValues.add("31");
    HexValues.add("32");
    HexValues.add("37");
    System.out.println("The content of HexValues is: " + HexValues);


    int start = HexValues.lastIndexOf("f");

    if (start != -1) {
        List<String> HexValuesEnd = HexValues.subList(start, HexValues.size());


        System.out.println("The content of HexValuesEnd before leaving is: " + HexValuesEnd);
        if (HexValuesEnd.size() > 0) {               

            HexValuesEnd.remove(1);
            HexValuesEnd.remove(2);
            HexValuesEnd.remove(3);
            System.out.println("The content of HexValuesEnd after removing values at indexes 1 ,2,3: " + HexValuesEnd);
        }
    }

出力は

The content of HexValues is: [a, b, f, 1, 4, 0, 31, 32, 37]
The content of HexValuesEnd  before leaving is: [f, 1, 4, 0, 31, 32, 37]
The content of HexValuesEnd after removing values at indexes 1 ,2,3: [f, 4, 31, 37]

ただし、2番目の配列リストの期待値は次のようになります。

"The content of HexValuesEnd after removing values at indexes 1 ,2,3: " [f,31,32,37]

期待される結果を得るのにどこが間違っているのでしょうか。

4

5 に答える 5

5

値の 1 つを削除すると、それ以降の値がシフトされてギャップが埋められます。

あなたがしようとしていることは

remove(1);
remove(1);
remove(1);
于 2013-01-03T05:32:59.427 に答える
1

後だからです

HexValuesEnd.remove(1);

配列リストは

[f、4、0、31、32、37]

これで実行されます

HexValuesEnd.remove(2);

だからあなたは得る

[f、4、31、32、37]

など...

あなたがする必要があるのは

HexValuesEnd.remove(1);
HexValuesEnd.remove(1);
HexValuesEnd.remove(1);
于 2013-01-03T05:36:18.807 に答える
0

持っていた[f, 1, 4, 0, 31, 32, 37]

次に、インデックス1で削除して取得します[f, 4, 0, 31, 32, 37]

次にインデックス 2: [f, 4, 31, 32, 37](インデックス 2 は0最初の削除後にリストにありました)

等々。

削除するとリストが変更されることに注意してください。

インデックスを13回削除したかったようです:

HexValuesEnd.remove(1);
HexValuesEnd.remove(1);
HexValuesEnd.remove(1);
于 2013-01-03T05:33:47.930 に答える
0

試す

 if (HexValuesEnd.size() > 0) {               
                HexValuesEnd.remove(1);
                HexValuesEnd.remove(1);
                HexValuesEnd.remove(1);
于 2013-01-03T05:37:41.433 に答える