Java 用一种方法比较多个对象以找到最大值?
嘿,我试图在我的主代码中找到一个方法,将类DVD的age属性与对象的所有其他实例进行比较,看看哪个属性最大,哪个属性最小。谁能把我引向正确的方向。如果有帮助,我可以发布main方法和其他类 DVD类别:Java 用一种方法比较多个对象以找到最大值?,java,class,object,methods,compare,Java,Class,Object,Methods,Compare,嘿,我试图在我的主代码中找到一个方法,将类DVD的age属性与对象的所有其他实例进行比较,看看哪个属性最大,哪个属性最小。谁能把我引向正确的方向。如果有帮助,我可以发布main方法和其他类 DVD类别: public class DVD { private String id; private String title; private String genre; private int age; public DVD(String id, String
public class DVD
{
private String id;
private String title;
private String genre;
private int age;
public DVD(String id, String title, String genre, int age)
{
this.id = id;
this.title = title;
this.genre = genre;
this.age = age;
}
public String getId()
{
return id;
}
public String getTitle()
{
return title;
}
public String getGenre()
{
return genre;
}
public int getAge()
{
return age;
}
//Setters
public void setID(String idIn)
{
id = idIn;
}
public void setTitle(String titleIn)
{
title = titleIn;
}
public void setGenre(String genreIn)
{
genre = genreIn;
}
public void setAge(int ageIn)
{
age = ageIn;
}
public String toString()
{
//Building string here
String x = "The id is: "+id+
"\nThe title is: "+title+
"\nThe genre is: "+genre+
"\nThe age is: "+age;
return x;
}
}//end of class
DVD商店类别:
public class DVDShop
{
//attributes (instance variables)
private DVD[] dvdList;
private int total;
//Constructors
public DVDShop (int maxNum)
{
dvdList = new DVD[maxNum];
total=0;
}
//accessor
public int getTotal()
{
return total;
}
// check if the list is full
public boolean isFull()
{
if (total == dvdList.length)
{
return true; // list is full
}
else
{
return false; // list is empty
}
}
// check if the list is empty
public boolean isEmpty()
{
if (total == 0)
{
return true; // list is empty
}
else
{
return false; // list is not empty
}
}
// add an item to the array
public boolean add(DVD DVDIn)
{
if (!isFull()) // check if list is full
{
dvdList[total] = DVDIn; // add item
total++; // increment total
return true; // indicate success
}
else
{
return false; // indicate failure
}
}
// helper method to find the id of a specified account
public int search(String dvdNumIn)
{
for(int i = 0; i < total; i++)
{
DVD tempDVD = dvdList[i]; // find the account at index i
String tempNumber = tempDVD.getId(); // get dvd number
if(tempNumber.equals(dvdNumIn))
{
return i;
}
}
return -999;
}
public boolean delete(String numberIn)
{
int index;
index = search(numberIn); // call the search method first
// if it could find the DVD it will return the element in the array that it is stored
// if not it will return a dummy value of -999
if(index == -999)
{
return false; // remove was unsuccessful
}
else
{
for(int i = index; i<= total-2; i++)
{
dvdList[i] = dvdList[i+1];
}
total--; // decrement total number of DVDs
return true; // remove was successful
}
}
public DVD getItem(String numIn)
{
int index;
index = search(numIn);
if(index == -999)
{
return null; // indicate invalid index
}
else
{
return dvdList[index];
}
}
}
在
DVD
类上实现Comparable
接口,如下所示:
public class DVD implements Comparable<DVD>
完成此操作后,您可以使用Arrays.sort(dvdList)
将收藏按年龄顺序排序。从这里开始,您只需挑选收藏中的第一个和最后一个元素,这两个元素将分别是您最旧的和最新的DVD
s
实际上,如果
的为null
,任何人都可以将我引向正确的方向,那么您可能需要在compareTo中添加一些null检查,以避免出现问题。
向DVDShop类添加查找最大和最小对象的方法,如下所示
public class DVDShop
{
private DVD[] dvdList;
---snip---
public int largestAge()
{
//find and return largest age from dvdList.
}
public int smallestAge()
{
//find and return smallest age from dvdList.
}
}
是否要按年龄对DVD对象集合进行排序?请发布您尝试的内容。告诉我们给定定义输入的预期输出。您对问题的描述不是很清楚:“将类DVD的年龄属性与对象的所有其他实例进行比较”并不重要。对不起,我的错误,我想搜索所有对象并找到最大年龄和最小年龄。添加,基本上,我想知道比较这十张DVD的代码是什么,然后打印最小值和最小值miximum@user2988501:您发布了许多不相关的代码,但没有显示您试图获得最小和最大年龄的内容。为什么不使用Collections.min()和Collections.max()?对列表进行排序只是为了获得最小值和最大值是浪费时间。@JBNizet OP将dvdList声明为数组,而不是集合,因此Collections.min()和Collections.max()在这里不适用,除非您还更改了dvdList的声明。然后他应该使用Collections.min(Arrays.asList(dvdList))
,或者从一开始就使用列表。从一开始就使用列表
会更好是的,但这不是实现目标的必要条件。我不同意他们应该调用Collections.min(Arrays.asList(dvdList))
或max等效项,因为这涉及到创建一个新的ArrayList
实例,将数组中的每个元素复制到其中,然后每次迭代新列表以查找min/max元素。排序允许的直接索引访问要快得多,特别是如果您经常这样做的话。Arrays.asList()不创建java.util.ArrayList实例,也不制作任何副本。它只是在原始数组上返回一个薄包装。阅读源代码和javadoc。它基本上与执行新整数(i)
一样快。
public int compareTo(DVD that) {
if (age > that.getAge()) {
// this is older than that
return 1;
} else if (age < that.getAge()) {
// this is newer than that
return -1;
}
// this is the same age as that
return 0;
}
public class DVDShop
{
private DVD[] dvdList;
---snip---
public int largestAge()
{
//find and return largest age from dvdList.
}
public int smallestAge()
{
//find and return smallest age from dvdList.
}
}