Why is this sort array not working?
I have to sort an array here is the code I have. When I try to utilize Arrays.sort() I get tons of errors. Anyone know what I am doing wrong? This is really my first time sorting and using arrays with constructors.
import java.util.Scanner;
import java.util.Arrays;
public class CoffeeDriver {
//main method
public static void main (String[] args){
Item[] itemObject = new Item[] {
new Item("Donut", .75),
new Item("Coffee", 1.00),
new Item("Bagel", 1.25),
new Item("Milk", 1.50),
new Item("Water", 2.00)};
Scanner input = new Scanner(System.in);
String decision;
System.out.println ("Welcome to Wings Coffee Shop");
System.out.println ("We have a great list of tasty items on our menu.");
System.out.println ("Would you like to see these items sorted by");
System.out.println ("name or by price? (n/p): ");
decision = input.nextLine();
sortName(itemObject);
sortPrice(itemObject);
}
//method to sort by item name and display
public static void sortName (Item[] itemObject){
Arrays.sort(itemObject);
System.out.println(itemObject);
}
//method to sort by item price and display
public static void sortPrice (Item[] array){
Arrays.sort(array);
for (int i = 0; i < array.length; i++){
System.out.println (array[i]);
}
}
}
public class Item
{
private String itemName = ""; //setting up the name
private double itemPrice=0.0; //Setting price variable
public Item(String name, double price) //Constructor
{
itemName = name;
itemPrice = price;
}
public String getitemName() //retuns name
{
return itemName;
}
public double getitemPrice() //returns price
{
retu开发者_StackOverflow中文版rn itemPrice;
}
public void setitemName(String name) //sets name
{
itemName = name;
}
public void setitemPrice (double price) //sets price
{
itemPrice = price;
}
}
Your Item class doesn't implement compareable. The sort method states: "Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array)."
But since you want to sort by two different things, implementing comparable won't get you very far, it's better to use a Comparator (http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html).
So your sort by name would look something like this (just replace the method part of compare(Item o1, Item o2) with the criteria you'd like the array to be sorted).
// method to sort by item name and display
public static void sortName(Item[] itemObject) {
Arrays.sort(itemObject, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
return o1.getitemName().compareTo(o2.getitemName());
}
});
printArr(itemObject);
}
and your sortPrice like that:
// method to sort by item price and display
public static void sortPrice(Item[] array) {
Arrays.sort(array, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
return Double.compare(o1.getitemPrice(), o2.getitemPrice());
}
});
printArr(array);
}
Also to print your results in a nice legible way it's generally a good idea to overwrite the toString() method in your class so that you can just write System.out.println(item); (toString is called automatically in that case).
Okay to get the content nicely printed we first add a toString() Method to your item class:
@Override
public String toString() {
return itemName + ": " + itemPrice;
}
That will just return the item's name and it's price when called. To print the array's content now nicely we'll just iterate through all it's members:
private static void printArr(Item[] arr) {
for(Item i : arr) {
System.out.println(i); // the same as System.out.println(i.toString()); - Java calls the toString method automatically in this case we you give it an object
}
}
Now we can just call this function everywhere you want to print the contents of an item array, like after sorting them!
Arrays.sort
takes a Comparator
which is used to perform the sort.
A Comparator
used to find the difference between 2 items.
public static void sortName (Item[] itemObject){
Arrays.sort(itemObject, new Comparator<Item>() {
public int compare(Item a, Item b) {
if(a.getitemName() == null){
return b.getitemName() == null ? 0 : -1;
}
return a.getitemName().compareTo(b.getitemName());
}
});
}
public static void sortPrice (Item[] array){
Arrays.sort(array, new Comparator<Item>() {
public int compare(Item a, Item b) {
return Double.compare(a.getitemPrice(), b.getitemPrice());
}
});
}
You need to implement the compareTo method in your Item class, and declare it to implement the Comparable interface.
See How to use Comparable for an example.
精彩评论