gpt4 book ai didi

java - 使用 compareTo() 对对象数组进行排序

转载 作者:搜寻专家 更新时间:2023-11-01 02:26:24 24 4
gpt4 key购买 nike

我可能会让这比现在更难......但我的任务是创建一个实现 Comparable 的放映时间类。有了这个,它构建了一个列表,其中包含几个不同的剧院,包括放映的电影、时间等。然后我必须使用 compareTo() 方法首先按放映的电影院和电影对它进行排序标题。我有以下代码......当我试图输出排序结果时会出现困惑。我把它放在通过比较字符串输出 - 或 + 数字的位置。

public class ShowTimeTest {


public static void main(String[] args) {
ShowTime[] movieTimes= new ShowTime[20];
// TODO Auto-generated method stub
//******************************************************
//Instantiates 20 showtime objects
//******************************************************



ShowTime cinemark1 = new ShowTime("Cinemark", "Lorde", "Friday @ 3:30", "Saturday @ 7:00", "Sunday @ 6:00","R", 1, 2);
ShowTime cinemark2 = new ShowTime("Amstar Cinemas", "Star Wars", "Thursday @ 12:00AM", "Monday @ 4:00PM", "Sunday @ 3:45PM","R", 1, 2);
ShowTime cinemark3 = new ShowTime("Fayette Movies", "Pokemon", "Friday @ 3:30", "Saturday @ 7:00", "Sunday @ 6:00","R", 1, 2);
ShowTime cinemark4 = new ShowTime("Dollar Theatre", "Reincarnated", "Friday @ 3:30", "Saturday @ 7:00", "Sunday @ 6:00","R", 1, 2);
ShowTime cinemark5 = new ShowTime("Rad Chads Cinemas", "Lorde", "Friday @ 3:30", "Saturday @ 7:00", "Sunday @ 6:00","R", 1, 2);
movieTimes[0] = cinemark1;
movieTimes[1] = cinemark2;
movieTimes[2] = cinemark3;
movieTimes[3] = cinemark4;
movieTimes[4] = cinemark5;

for(int i = 1; i < 5; i ++){
System.out.println(movieTimes[i].compareTo(movieTimes[i-1]));

}
}
}

续...

public class ShowTime implements Comparable<ShowTime>{

public String name, title, rating, showTime1, showTime2, showTime3;
public double ticket, adultTicket;
public String[] times = new String[3];

public ShowTime(String theatreName, String movieTitle, String showTime1, String showTime2, String showTime3 , String movieRating, double childTicket, double aTicket)
{
// TODO Auto-generated constructor stub
name = theatreName;
title = movieTitle;
times[0] = showTime1;
times[1] = showTime2;
times[2] = showTime3;
rating = movieRating;
ticket = childTicket;
adultTicket = aTicket;
}
public String toString(){
String out = "Theatre: ";
out += name+ " " + "Movie: " + title + " " + "Rating " + rating + " " + "ShowTimes " + times[0] + " " + times[1] + " " + times[2]
+ " " + "Adult Cost: " + adultTicket + " " + "Child Ticket: " +ticket;
return out;
}
public int compareTo(ShowTime other) {
// TODO Auto-generated method stub
//sorts the showtimes by theater(NAME) and movie(TITLE)
int result;

if(name.equals(((ShowTime)other).name)){
result = title.compareTo(((ShowTime)other).title);
}

else{
result = name.compareTo(((ShowTime)other).name);
}
return result;
}
}

最佳答案

让您的类实现Comparable 接口(interface)的目的是让您可以对ShowTime 的数组进行排序。你已经完成了所有艰苦的工作。现在申请Arrays.sort ,坐下来欣赏电影。

由于这是一个教育节目,让我 100% 清楚地说明:您已经有了一个 Object[],其元素实现了 Comparable,即 movieTimes 。因此,您需要做的就是添加一行:

Arrays.sort(movieTimes);

请注意,这将失败,直到您用 20 部电影装满您的影院(或将其声明为仅容纳 5 部电影的迷你影院)。

关于java - 使用 compareTo() 对对象数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22058272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com