Java 不使用接口对自定义对象的ArrayList进行排序
很抱歉,这可能是重复的,我不确定我之前的尝试是否成功 几周前开始学习Java,正在完成我的第一个作业之一。:) 我的问题有点基本,但在浏览了以前解决的主题后,我找不到完全相同的问题。这不是现实生活中的问题,所以我想我应该用一种非常具体的方式来解决它 因此,任务由几个步骤组成——我必须创建一个包含大量自定义对象的超类,添加新的子类,实现新的方法来计算某些变量的值,编写测试类,并对输出进行排序 除了这最后一步,一切都完成了。我不确定我是否可以在网上发布我的问题,但我现在就在这里: 我有点像:Java 不使用接口对自定义对象的ArrayList进行排序,java,sorting,object,arraylist,Java,Sorting,Object,Arraylist,很抱歉,这可能是重复的,我不确定我之前的尝试是否成功 几周前开始学习Java,正在完成我的第一个作业之一。:) 我的问题有点基本,但在浏览了以前解决的主题后,我找不到完全相同的问题。这不是现实生活中的问题,所以我想我应该用一种非常具体的方式来解决它 因此,任务由几个步骤组成——我必须创建一个包含大量自定义对象的超类,添加新的子类,实现新的方法来计算某些变量的值,编写测试类,并对输出进行排序 除了这最后一步,一切都完成了。我不确定我是否可以在网上发布我的问题,但我现在就在这里: 我有点像: pub
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);
import java.util.ArrayList;
公开选课
{
公共静态void main(字符串[]args){
牛仔裤ex1=新牛仔裤(“约翰·刘易斯”);
例1.原产国(“美国”);
ex1.颜色(“海军蓝”);
ex1.setSize(Pants.SizeType.LARGE);
ex1.设定价格(40);
ex1.机器可损坏(真实);
系统输出打印项次(ex1);
短裤ex2=新短裤(“Ted Baker”);
ex2.原产国(“联合王国”);
ex2.颜色(“白色”);
ex2.setSize(裤子、尺码类型、中等);
ex2.设定价格(30);
ex2.机器易损坏(正确);
系统输出打印项次(ex2);
//等等
ArrayList selection=新建ArrayList();
选择.添加(ex1);
选择.添加(ex2);
选择.添加(ex3);
选择.添加(ex4);
选择.添加(ex5);
System.out.println(“大小-大:”);
System.out.println();
裤子。尺码尺码;
尺寸=Pants.SizeType.LARGE;
列表裤(选择、尺寸);
我需要编写一个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());
publicstaticvoidlistpants(arraylistselection,Pants.SizeType-size)
{
对于(Pants.SizeType SizeType:Pants.SizeType.values()){
用于(裤子:选择){
if(pants.getSize()等于(sizeType)){
System.out.println(selection.toString());
我假设您的类无法实现新接口,并且根本不使用接口
您可以使用,它是为您的类构建的
差不多
Collections.sort(selection,new Comparator<Pants>() {
@Override
public int compare(Pants p1, Pants p2) {
//implement your compare method in here
...
}
});
Collections.sort(选择,新建比较器(){
@凌驾
公共整数比较(裤子p1,裤子p2){
//在这里实现您的比较方法
...
}
});
如果您渴望创建自己的排序算法,请看一看。IMO最简单的实现方法(尽管速度很慢)是我认为这只是您面临的一个小问题。您已经定义了该方法的签名,该方法应打印出特定尺寸的所有裤子:
ListPants(ArrayList<Pants> selection, Pants.SizeType size)
ListPants(ArrayList选择,Pants.SizeType大小)
没错。现在,您的代码在所有裤子和所有可能的尺码上循环:
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());
publicstaticvoidlistpants(arraylistselection,Pants.SizeType-size)
{
对于(Pants.SizeType SizeType:Pants.SizeType.values()){
用于(裤子:选择){
if(pants.getSize()等于(sizeType)){
System.out.println(selection.toString());
因为这看起来像是一个家庭作业,所以我将把我的答案表述为一个问题:
您在
ListPants
的方法体中使用size
参数的位置在哪里?谢谢您的快速回复!是的,这也是我从其他类似的线程中收集到的。但是,由于这是家庭作业,我不认为这是完成此特定作业的一个选项。@Latthargicwasp请参阅编辑(答案的最后一部分)我链接了一些排序算法,也是最容易实现的算法,感谢您的输入!我认为这是正确的语法:if(pants.getSize().equals(sizeType)){是否应该简单地更改为if(size.equals(sizeType)){?这里是深夜。:)完全删除第一个循环。比较pants.getSize()和大小。