Values ​​in the Java Queue

If I have a queue with strings as values, how would I print all the values ​​that I used:

System.out.println(queue.elements().toString().); 

But does it print java objects ...?

Do I need to use a loop to print queue values?

+4
source share
6 answers

Yes, you will need to use a loop, but the loop can be simple.

 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.

+8
source

The answer depends on the type of queue .

  • If it is a queue or Deque , then your code will not compile because these interfaces do not define the elements() method. This applies to most classes that implement Collection .

  • If it's Vector , then elements() returns Enumeration , and you need to use a loop to get the values ​​out of it.

My advice:

  • Stop using Vector ... if you have no choice. A vector is an inherited class. Instead, use one of the queue or Deque .

  • If you use Vector , use queue.toString() , not queue.elements().toString() , to display the contents of the queue as a String. The toString() method is defined as rendering collection elements for all standard collection classes.

+1
source

I do not understand your question. You say you want to print lines, and then after some code that does not 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 } } 

Launch, for example, java Foo apple banana pear . Output:

 [apple, banana, pear] apple banana pear 

If the problem is that you get the output as follows:

 [ MyObject@498b5a73 , MyObject@5bdf59bd , MyObject@247cb66a ] MyObject@498b5a73 MyObject@5bdf59bd MyObject@247cb66a 

Then your queue does not actually contain a string, and you forgot 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 [like, this, output, format] .

+1
source

Try the following:

  Enumeration e = queue.elements(); while ( e.hasMoreElements() ) System.out.println( e.nextElement() ); 
0
source

Take a look at Joiner in guava.

 System.out.println(Joiner.on("\n").join(queue.elememts())); 
0
source

try it

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); 

}

}

0
source

Source: https://habr.com/ru/post/1345026/


All Articles