TeeMee123
TeeMee123

Reputation: 161

Convert Array to Queue

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

Answers (2)

Oguz
Oguz

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

Rahul Chowdhury
Rahul Chowdhury

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

Related Questions