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

我可能会比这更难……但我的任务是创建一个实现Comparable的showtimes类.有了这个,它构建了几个不同剧院的列表,其中包括时间等等.然后我必须使用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{

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值