简体   繁体   中英

Sorting objects in an arraylist by one of the objects

I have found this question so many times, I have tried all of their solutions and when I submit them my code fails for "Null Pointer Exception"

   /**
 * q5: Create a public class named "Fleet" with private instance variables "considerable" and 
 * "complicated" each of type int. You may add any other methods and variables you'd like to 
 * this class.
 * 
 * Outside of Fleet (in the Problem Set class) write a public static method named "sortFleet" 
 * that takes an ArrayList of Fleets as a parameter and returns void. This method will sort 
 * the input by the variable "complicated" in increasing order
 */
  public class Fleet {
private int considerable, complicated;

public Fleet(int beginning) {
    this.complicated = beginning;
}

public int getPrecious() {
    return this.complicated;
}
@Override 
public String toString() {
    return "" + complicated;
}
  }

public void sortFleet(ArrayList<Fleet> a) {

Collections.sort(a, new Comparator<Fleet>(){    
    @Override
    public int compare(Fleet o1, Fleet o2) {
        Long a1 = new Long(o1.getPrecious());
        Long a2 = new Long(o2.getPrecious());
        //return a1.compareTo(a2);  
        return Long.compare(a1,a2);

    }

}); 
 }

The code works and I have done so many different ways of this and it works. but when I submit it says the same thing. every single time. Please help

To sort Fleet by Complicated, you have to make the Object implement the Comparable interface and override the compareTo() method as shown bellow.

Fleet Class:

public class Fleet implements java.lang.Comparable<Fleet>{

    private int considerable, complicated;

    public Fleet() {
        super();
    }

    public Fleet(int considerable, int complicated) {
        super();
        this.considerable = considerable;
        this.complicated = complicated;
    }

    public int getConsiderable() {
        return considerable;
    }

    public void setConsiderable(int considerable) {
        this.considerable = considerable;
    }

    public int getComplicated() {
        return complicated;
    }

    public void setComplicated(int complicated) {
        this.complicated = complicated;
    }

    @Override
    public int compareTo(Fleet fleet) {
        int compareComplicated = ((Fleet)fleet).getComplicated();
        return this.complicated - compareComplicated;
    }

    @Override
    public String toString() {
        return "Fleet [considerable=" + considerable + ", complicated=" + complicated + "]";
    }


}

Now we can call Collections.sort on ArrayList:

Main Class:

import java.util.ArrayList;
import java.util.Collections;

public class SortFleet {

    public static void main(String[] args) {
        ArrayList<Fleet> arrayFleet = new ArrayList<Fleet>();
        Fleet fleetA = new Fleet(1, 2);
        Fleet fleetB = new Fleet(4, 12);
        Fleet fleetC = new Fleet(3, 5);
        Fleet fleetD = new Fleet(3, 50);
        Fleet fleetE = new Fleet(2, 3);
        Fleet fleetF = new Fleet(2, 50);


        arrayFleet.add(fleetA);
        arrayFleet.add(fleetB);
        arrayFleet.add(fleetC);
        arrayFleet.add(fleetD);
        arrayFleet.add(fleetE);
        arrayFleet.add(fleetF);


        System.out.println("Before sort:");

        for ( Fleet f : arrayFleet)
        {
            System.out.println(f);
        }

        System.out.println();

        sortFleet(arrayFleet);

        System.out.println("After sort:");
        for ( Fleet f : arrayFleet)
        {
            System.out.println(f);
        }
    }

    public static void sortFleet(ArrayList<Fleet> arrayFleet) {
        Collections.sort(arrayFleet);
    }

}

Console Output:

Before sort:
Fleet [considerable=1, complicated=2]
Fleet [considerable=4, complicated=12]
Fleet [considerable=3, complicated=5]
Fleet [considerable=3, complicated=50]
Fleet [considerable=2, complicated=3]
Fleet [considerable=2, complicated=50]

After sort:
Fleet [considerable=1, complicated=2]
Fleet [considerable=2, complicated=3]
Fleet [considerable=3, complicated=5]
Fleet [considerable=4, complicated=12]
Fleet [considerable=3, complicated=50]
Fleet [considerable=2, complicated=50]

EDIT: JAVA 8 APPROACH:

Removing Comparable interface and the override in the Fleet class you could just sort like this, returning the same expected output:

public static void sortFleet(ArrayList<Fleet> arrayFleet) {
        Collections.sort(arrayFleet, Comparator.comparingInt(Fleet::getComplicated));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM