简体   繁体   中英

Sorting Arraylists of custom objects without using interfaces

Sorry, this might be duplicated, I'm not sure if my previous attempt to post this went through.

Started to learn Java several weeks ago, working on one of my first assignments. :)

My question is somewhat basic, but I couldn't find its exact equivalent after looking through previously resolved topics. This isn't a real life problem, so I guess it's expected from me to tackle it in a very specific way.

So the task consisted of several steps - I had to create a superclass with a number of custom objects, add new subclasses, implement new methods to count the value of certain variables, write test classes and sort my output.

It's all been done apart from this last step. Not sure if I'm allowed to just post my problems like that on the web, but here is where I am right now:

I have something like:

public class Pants
{
public enum SizeType {SMALL, MEDIUM, LARGE, EXTRA_LARGE}
private SizeType size; 
private String brand;
private String countryOfOrigin;
private String color;
private double price;

//Other variables and methods

}

public class Jeans extends Pants  
{ 
//new variables and methods 
}


public class Shorts extends Pants 
{
//some more new variables and methods
}

And other similar subclasses.

import java.util.ArrayList;
public class Selection
{
public static void main(String[] args){

    Jeans ex1 = new Jeans("John Lewis");
    ex1.countryOfOrigin("US");
    ex1.color("Navy");
    ex1.setSize(Pants.SizeType.LARGE);
    ex1.setprice(40);
    ex1.machineWashable(true);
    System.out.println(ex1);

    Shorts ex2 = new Shorts("Ted Baker");
    ex2.countryOfOrigin("United Kingdom");
    ex2.color("White");
    ex2.setSize(Pants.SizeType.MEDIUM);
    ex2.setprice(30);
    ex2.machineWashable(true);
    System.out.println(ex2);
//..etc

ArrayList<Pants> selection = new ArrayList<Pants>();
    selection.add(ex1);
    selection.add(ex2);
    selection.add(ex3);
    selection.add(ex4);
    selection.add(ex5);

    System.out.println( "Size - LARGE: " );
    System.out.println();
    Pants.SizeType size;
    size = Pants.SizeType.LARGE;
    ListPants(selection,size);

I need to write a ListPants method to list objects depending on SizeType - starting with large in this case. I don't think I can implement any additional interfaces (which is what was mostly recommended in other threads).

Please see my attempt below (didn't work). Am I thinking in the right direction here, or?

public static void ListPants(ArrayList<Pants> selection, Pants.SizeType size)
{
for (Pants.SizeType sizeType : Pants.SizeType.values()) {
    for (Pants pants : selection) {
        if (pants.getSize().equals(sizeType)) {
System.out.println(selection.toString());    

I think it's just a minor problem you're facing. You already defined the signature of the method which should print out all pants of a specific size:

ListPants(ArrayList<Pants> selection, Pants.SizeType size)

That is correct. Now, your code is looping over all pants and over all possible sizes:

public static void ListPants(ArrayList<Pants> selection, Pants.SizeType size)
{
for (Pants.SizeType sizeType : Pants.SizeType.values()) {
    for (Pants pants : selection) {
        if (pants.getSize().equals(sizeType)) {
           System.out.println(selection.toString()); 

Since this looks like a homework assignment, i'll phrase my answer as a question:

Where are you using the size parameter in the method body of ListPants ?

I am assuming your class cannot implement new interfaces, and not using interfaces at all.

You can use Collections.sort(List,Comparator) with a Comparator , which is built for your class.

Something like

Collections.sort(selection,new Comparator<Pants>() { 

   @Override
   public int compare(Pants p1, Pants p2) { 
       //implement your compare method in here
       ...
   }
});

If you are eager to create your own sorting algorithm, have a look of this list of sorting algorithms . Simplest to implement (though pretty slow) IMO is selection-sort

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