Reputation: 161
I wish to create a Queue (or Stack) in java using all the elements from an array. Is there some 'nice' way of doing this, i.e in one line without a loop over the array?
Upvotes: 13
Views: 24678
Reputation: 1926
For stacks, you should create a vector object, because stack extends Vector class.
Stack<Object> stack = (Stack<Object>) new Vector(Arrays.asList(theArray));
Upvotes: 4
Reputation: 681
This should work. yourArray is the input array. Substitute Object for whatever data type you're dealing with.
Queue<Object> queue = new LinkedList<>(Arrays.asList(yourArray));
Upvotes: 27