In the method queueAnalysis
, when I command to print System.out.println("Original: " + queue + "\n");
, System.out.println("colados: " + names + "\n");
and
System.out.println("Cola final: " +queue);
, it gives me as output direcciones de memoria
, null
and direcciones de memoria
, I already tried to apply a iterator
and apply a class type casting Customer
but even so it continues to give memory addresses as output. Why?,
How can I fix the problem?
class Customer {
String name;
int ticket;
Customer(String name, int ticket) {
this.name = name;
this.ticket = ticket;
}
String getName() {
return name;
}
int getTicket() {
return ticket;
}
}
public class Main {
public static void main(String[] args) {
Queue<Customer> queue = new LinkedList<>();
queue.add(new Customer("a1", 1));
queue.add(new Customer("a2", 2));
queue.add(new Customer("a3", -1));
queue.add(new Customer("a4", -1));
queueAnalysis(queue);
}
public static void queueAnalysis(Queue<Customer> queue) {
System.out.println("Original: " + queue + "\n");
String names[] = null;
int cant = 0;
int tam = queue.size();
for (int i = 0; i < tam; i++) {
if (queue.peek().getTicket() == -1) {
names[i] = queue.peek().getName();
cant++;
queue.poll();
}
}
System.out.println("colados: " + names + "\n");
System.out.println("Cola final: " +queue);
//Customer c;
for (Iterator it = queue.iterator(); it.hasNext();) {
System.out.println((Customer) it.next());
}
}
}
You can't print an arrayList directly or a vector:
If what you want is to show the name of the objects or something else, simply override the ToString() method of the Customer class.
In addition to the answer given by @Edu3D, you must change the way you get the data from the queues and the
fila
final one, this is because you are not accessing the queue by indices and when you dopeek()
orpoll()
it is always on the object that is in thehead
de the row, here I modify the methodThis only works if you make the changes mentioned by @Edu3D to override the
toString()
class methodCustomer