How to print class data?

1

In the method queueAnalysis , when I send to print System.out.println("Original: " + queue + "\n"); , System.out.println("colados: " + names + "\n"); and          System.out.println("Cola final: " +queue); , gives me as output direcciones de memoria , null and direcciones de memoria , and try to apply a iterator and apply a casting of type class Customer but still keeps giving memory addresses as output. Why ?,

How can I solve 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());
    }

}

}
    
asked by Michelle 21.09.2017 в 17:53
source

3 answers

2

In addition to the answer given by @ Edu3D, you must change the way to obtain the data of the castings and the fila final%, this to which you are not accessing the queue by indexes and when you do peek() or poll() is always about the object that is in the head of the row, here I modify the method

public static void queueAnalysis(Queue<Customer> queue) {
  System.out.println("Original: " + queue + "\n");
  Queue<Customer> colados = new LinkedList<>();
  Queue<Customer> noColados = new LinkedList<>();
  while(!queue.isEmpty()){
    if (queue.peek().getTicket() == -1) {
      colados.add(queue.poll());
    }else{
      noColados.add(queue.poll());
    }
  }
  System.out.println("colados: " + colados + "\n");
  System.out.println("Cola final: " + noColados);
  //Customer c;
  for (Iterator it = queue.iterator(); it.hasNext();) {
    System.out.println((Customer) it.next());
  }
}

This only works if you make the changes mentioned by @ Edu3D to overwrite the method toString() of class Customer

public class Customer {
  String name;
  int ticket;

  public Customer(String name, int ticket) {
    this.name = name;
    this.ticket = ticket;
  }
  String getName() {
    return name;
  }
  int getTicket() {
    return ticket;
  }

  @Override
  public String toString(){
    return getName()+ "-" + getTicket();        
  }
}
    
answered by 21.09.2017 / 18:32
source
2

You can not print an arrayList directly or a vector:

for( Customer c: queue ){
  System.out.println( "Original: " + c.getName( ) + "" + c.getTicket( ));
}
    
answered by 21.09.2017 в 18:07
2

If you want to show the name of the objects or something else simply overwrite the ToString () method of the Customer class

public class Customer {

    String name;
    int ticket;

    public Customer(String name, int ticket) {
        this.name = name;
        this.ticket = ticket;

    }

    String getName() {
        return name;

    }

    int getTicket() {
        return ticket;
    }


    @Override
    public String toString(){
        return getName()+ "-" + getTicket();        
    }
}
    
answered by 21.09.2017 в 18:31