【问题标题】:Sorting Arraylists of custom objects without using interfaces不使用接口对自定义对象的 Arraylist 进行排序
【发布时间】:2012-11-26 19:33:52
【问题描述】:

抱歉,这可能是重复的,我不确定我之前发布的尝试是否通过了。

几周前开始学习 Java,完成了我的第一个任务。 :)

我的问题有点基本,但在查看了以前解决的主题后,我找不到它的完全等价物。这不是现实生活中的问题,所以我想我应该以非常具体的方式解决它。

所以任务包括几个步骤 - 我必须创建一个包含许多自定义对象的超类、添加新的子类、实现新方法来计算某些变量的值、编写测试类并对我的输出进行排序。

除了最后一步之外,所有这些都已完成。不确定我是否可以在网上发布类似的问题,但这是我现在的位置:

我有类似的东西:

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
}

和其他类似的子类。

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);

我需要编写一个 ListPants 方法来根据 SizeType 列出对象 - 在这种情况下从 large 开始。我不认为我可以实现任何额外的接口(这是其他线程中最推荐的)。

请在下面查看我的尝试(无效)。我在这里思考的方向是正确的,还是?

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());    

【问题讨论】:

    标签: java sorting object arraylist


    【解决方案1】:

    我假设您的类无法实现新接口,并且根本不使用接口。

    您可以将Collections.sort(List,Comparator) 与专为您的班级构建的Comparator 一起使用。

    类似

    Collections.sort(selection,new Comparator<Pants>() { 
    
       @Override
       public int compare(Pants p1, Pants p2) { 
           //implement your compare method in here
           ...
       }
    });
    

    如果您渴望创建自己的排序算法,请查看list of sorting algorithms。最简单的实现(虽然很慢)IMO 是 selection-sort

    【讨论】:

    • 感谢您的快速回复!是的,这也是我从其他类似线程中收集到的。但是,由于这是家庭作业,我认为这不是完成这项特定任务的选择。
    • @lethargicwasp 查看编辑(答案的最后部分)我链接到一些排序算法,并且最容易实现 IMO
    【解决方案2】:

    我认为这只是您面临的一个小问题。您已经定义了应该打印出特定尺寸的所有裤子的方法的签名:

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

    没错。现在,您的代码正在遍历所有裤子和所有可能的尺寸:

    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()); 
    

    由于这看起来像是一项家庭作业,我将把我的答案表述为一个问题:

    ListPants的方法体中size参数在哪里使用?

    【讨论】:

    • 感谢您的意见!我认为这是正确的语法: if (pants.getSize().equals(sizeType)) { 是否应该简单地更改为 if (size.equals(sizeType)){ ?在这里深夜。 :)
    • 完全删除第一个循环。比较pants.getSize() 和size。
    猜你喜欢
    • 2010-12-21
    • 2019-07-26
    • 2012-05-10
    • 2011-02-16
    相关资源
    最近更新 更多