Reputation: 25058
If I have a queue with strings as values How would I print all the values I was using:
System.out.println(queue.elements().toString().);
But it prints java objects...?
Do I have to use a loop to print values of queue?
Upvotes: 10
Views: 54395
Reputation: 16399
I don't understand your question. You say you want to print strings, and then after some code that doesn't compile, you say, "But it prints java objects...?" Strings are Java objects.
import java.util.*;
public class Foo {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<String>(); // create queue
for (String name : args) queue.add(name); // add strings to queue
System.out.println(queue); // print entire queue at once
for (String s : queue) System.out.println(s); // print each separately
}
}
Run with e.g., java Foo apple banana pear
. The output is:
[apple, banana, pear]
apple
banana
pear
If the problem is that you are getting output like this:
[MyObject@498b5a73, MyObject@5bdf59bd, MyObject@247cb66a]
MyObject@498b5a73
MyObject@5bdf59bd
MyObject@247cb66a
Then your Queue does not actually hold Strings, and you have forgotten to override the toString()
method in your class:
@Override public String toString() {
return firstName + " " + lastName + ", " + quest + ", " + favoriteColor;
}
There is no need to use a loop, unless you don't [like, this, output, format]
.
Upvotes: 4
Reputation: 719446
The answer depends on the type of queue
.
If it is a Queue
or Deque
, then your code won't compile, because these interfaces don't define an elements()
method. This applies to most of the classes that implement Collection
.
If it is a Vector
, then the elements()
returns an Enumeration
, and you have to use a loop to pull the values from it.
My advice would be:
Stop using Vector
... unless you have no choice. Vector is a legacy class. Use one of the implementations of Queue
or Deque
instead.
Whether or not you use Vector
, use queue.toString()
rather than queue.elements().toString()
to render the queue contents as a String. The toString()
method is defined as rendering the elements of the collection for all of the standard collection classes.
Upvotes: 1
Reputation: 9478
try this
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue myQueue = new LinkedList();
myQueue.add("A"); myQueue.add("B"); myQueue.add("C"); myQueue.add("D"); List<String> myList = new ArrayList<String>(myQueue); for (Object theFruit : myList) System.out.println(theFruit);
}
}
Upvotes: 0
Reputation: 25150
Look at Joiner in guava.
System.out.println(Joiner.on("\n").join(queue.elememts()));
Upvotes: 0
Reputation: 4423
Yes, you will need to use a loop however the loop can be simple like.
for(String s : queue) {
System.out.println(s.toString());
}
Actually, as long as it implements Iterable you should be able to do this type of foreach loop.
Upvotes: 15
Reputation: 6093
Try this:
Enumeration e = queue.elements();
while ( e.hasMoreElements() )
System.out.println( e.nextElement() );
Upvotes: 0