java howto ArrayListプッシュ、ポップ、シフト、およびシフト解除


88

JavaArrayList.addはJavaScriptに似ていると判断しましたArray.push

私はArrayList次のような関数を見つけることに固執しています

  • Array.pop
  • Array.shift
  • Array.unshift 私はに傾いています ArrayList.remove[At]

回答:


142

ArrayList命名基準がユニークです。同等のものは次のとおりです。

Array.push    -> ArrayList.add(Object o); // Append the list
Array.pop     -> ArrayList.remove(int index); // Remove list[index]
Array.shift   -> ArrayList.remove(0); // Remove first element
Array.unshift -> ArrayList.add(int index, Object o); // Prepend the list

要素を削除するのでunshiftはなく、リストに追加することに注意してください。また、JavaとJSにはそれぞれ独自の標準があるため、コーナーケースの動作が異なる可能性があることにも注意してください。


9
多くの「シフト解除」を行っているが、中間インデックスでの取得はそれほど多くない場合、実際の実行時間の点でArrayListがLinkedListより劣っていることがあります。
パトリック

while(Item item = items.remove(0)){...}はshiftと同等ではありません。
e-info128 2015

どう.pushですか?
jameshfisher 2016年

1
OPは、彼が知っていたと述べArray.push -> ArrayList.add、具体的には、について尋ねpopshiftunshift。これをもう一度読ん.pushで、説明を追加すると同時に追加します。
Jon Egeland 2016年

尋ねられなかったとしても、これらの機能の複雑さについて言及しなければ、この答えは不完全だと感じます。
ジャスパー

25

私はしばらく前にこの問題に直面していましたjava.util.LinkedListが、私の場合に最適であることがわかりました。いくつかのメソッドがあり、名前が異なりますが、必要なことを実行しています。

push()    -> LinkedList.addLast(); // Or just LinkedList.add();
pop()     -> LinkedList.pollLast();
shift()   -> LinkedList.pollFirst();
unshift() -> LinkedList.addFirst();

1
なぜこれが受け入れられないのですか?!注:インターフェースLinkeListに非常に非効率的なメソッドを追加ArrayListListます。これが私を混乱させました。このメソッドは、それが実装するDequeおよびQueueインターフェースから取得されますが、ArrayListそうではありません。
CiroSantilli郝海东冠事病六四事件法轮功2015年

1
@CiroSantilli新疆改造中心六四事件法轮功ですが、どれだけ非効率的ですか?
スラヴァ2018

@Slava O(n)vs O(1)フロントインサートは巨大です。
CiroSantilli郝海东冠事病六四事件法轮功

3
@CiroSantilli新疆改造中心六四事件法轮功O(n)とO(1)は単なる複雑さです。リンクリストは、挿入/削除の場合でも配列リストよりもかなり遅くなる可能性があると聞きました。stackoverflow.com/questions/34170566/…では、Javaはどうでしょうか。
スラヴァ2018

14

多分あなたは見てjava.util.Stackクラスを取りたいです。プッシュ、ポップメソッドがあります。リストインターフェイスを実装しました。

シフト/シフト解除については、@ Jonの回答を参照できます。

ただし、気にかけたいArrayListの何か、arrayListは同期されていません。しかし、スタックはそうです。(Vectorのサブクラス)。スレッドセーフな要件がある場合は、StackがArrayListよりも優れている可能性があります。



悪いことに、睡眠不足の状態で後半を読んでいないことに気づきました。
MJレイバーン

3

ジョンによる素晴らしい答え。

私は怠け者で、タイピングが嫌いなので、私のような他のすべての人々のために簡単なカットアンドペーストの例を作成しました。楽しい!

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        List<String> animals = new ArrayList<>();

        animals.add("Lion");
        animals.add("Tiger");
        animals.add("Cat");
        animals.add("Dog");

        System.out.println(animals); // [Lion, Tiger, Cat, Dog]

        // add() -> push(): Add items to the end of an array
        animals.add("Elephant");
        System.out.println(animals);  // [Lion, Tiger, Cat, Dog, Elephant]

        // remove() -> pop(): Remove an item from the end of an array
        animals.remove(animals.size() - 1);
        System.out.println(animals); // [Lion, Tiger, Cat, Dog]

        // add(0,"xyz") -> unshift(): Add items to the beginning of an array
        animals.add(0, "Penguin");
        System.out.println(animals); // [Penguin, Lion, Tiger, Cat, Dog]

        // remove(0) -> shift(): Remove an item from the beginning of an array
        animals.remove(0);
        System.out.println(animals); // [Lion, Tiger, Cat, Dog]

    }

}

2

アンダースコア-Javaライブラリには、メソッドpush(values)、pop()、shift()、およびunshift(values)が含まれています。

コード例:

import com.github.underscore.U:

List<String> strings = Arrays.asList("one", "two", " three");
List<String> newStrings = U.push(strings, "four", "five");
// ["one", " two", "three", " four", "five"]
String newPopString = U.pop(strings).fst();
// " three"
String newShiftString = U.shift(strings).fst();
// "one"
List<String> newUnshiftStrings = U.unshift(strings, "four", "five");
// ["four", " five", "one", " two", "three"]
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.