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?
Asked
Active
Viewed 2.2k times
12
-
suppose this [post](https://stackoverflow.com/questions/23485944/how-to-convert-list-to-queue-to-achieve-fifo) may be helpful to get started. – Rajith Pemabandu Jul 30 '17 at 02:05
-
View this post for help in the link below. https://stackoverflow.com/questions/41153502/convert-queue-into-long-array – Mustajeeb ur Rehman Jul 30 '17 at 03:02
2 Answers
24
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));

Rahul Chowdhury
- 641
- 1
- 7
- 16
3
For stacks, you should create a vector object, because stack extends Vector class.
Stack<Object> stack = (Stack<Object>) new Vector(Arrays.asList(theArray));

Oguz
- 1,867
- 1
- 17
- 24