Tuesday 15 July 2014

android - Java data structure for reverse insert -


Is there a datastructure in Java that supports reverse inserting? I want to add value to a datastructure I do not know how many values ​​in advance should be inserted in the following line:

  datastructure.insert (1); // datastructure is: {1} datastructure.insert (4); // Datastructure is: {4,1} datastructure.int (9); // datastructure is: {9,4,1}   

The last value should always be the first. Which data structure is the best suit?

The best approach is that you do not use the stack, you actually have an older version of Java A heap btvview will essentially make you the opposite order.

  stack stack = new stack (); Stack.push (1); Stack.push (4); Stack.push (9); System.out.println ("stack:" + stack); Deque & LT; Integer & gt; Deck = new ArrayDek & lt; & Gt; (); Deque.addFirst (1); Deque.addFirst (4); Deque.addFirst (9); System.out.println ("deck:" + deck);   

print

  stack: [1, 4, 9] decks: [9, 4, 1]    

No comments:

Post a Comment