0

How can I add a quick sort to this program and not return the [Ljava.lang.Object;@5dfcfece with the results?

The program should provide the contents of the queue and then sort the queue using the quick sort in two ways:

Descending order by last name. Descending order by age.

import java.util.LinkedList;
import java.util.Scanner;

public class personQueue

{
private static Scanner scnr;

public static void main(String[] args)

{

    LinkedListtheQ = new LinkedList < > ();
    scnr = new Scanner(System.in);
    int i;
    for (i = 0; i < 5; i++) {
      System.out.println("Enter the fist name of the person in queue position " + (i + 1) + ":\n");
      String first = scnr.next();
      System.out.println("Enter the last name of the person in queue position " + (i + 1) + ":\n");
      String last = scnr.next();
      System.out.println("Enter the age of the person in queue position " + (i + 1) + ":\n");
      int age = scnr.nextInt();
      theQ.add(new Person(first, last, age));
    }
    System.out.print(theQ.toArray());
    for (int j = 0; j < theQ.toArray().length; j++)
    {
      System.out.println(theQ.toArray()[j].toString());
    }
}
}
public class Person implements Comparable < Person > {
String first;
String last;
int age;

public Person(String firstName, String lastName, int age) {
    this.first = firstName;
    this.last = lastName;
    this.age = age;
}

public String toString() {;
      return "\n " + this.first + " " + this.last + " " + this.age + "\n ";
}
public int compareTo(Person arg0) {
    return 0;
}
  • 1
    You should call `theQ.toArray()` just once, and assign it to a variable. Every time you call it, it iterates through the list and creates a brand new array. It's inefficient to call it multiple times. Also, use an ArrayList as it's more efficient than LinkedList. – k314159 Mar 04 '21 at 17:29
  • 1
    `Ljava.lang.Object;@5dfcfece` is result of `System.out.print(theQ.toArray());` since arrays don't override its toString method and use version inherited from Object class which simply returns `typeInfo@hexHash`. If you want to print array take a look at [What's the simplest way to print a Java array?](https://stackoverflow.com/q/409784) – Pshemo Mar 04 '21 at 17:31
  • 1
    Or simply `System.out.print(theQ);` since Lists print their contents properly. – k314159 Mar 04 '21 at 17:34

0 Answers0