按变量排序元组

时间:2014-01-19 23:47:05

标签: arrays list sorting collections tuples

我正在做以下活动: 目标是制定一个管理成员名单的计划 在跳远比赛中。可用的地方数量是15。 他们的数据将以与运动员相同的顺序介绍 注册。 设计一个显示以下选项的程序:

1 - 注册参与者

2 - 列出所有参与者的数据

3 - 按标记

列出所有参与者的数据

4 - 退出 如果选择1,将引入其中一个参与者的数据: 名称,2012年最佳成绩,2011年最佳成绩,2010年最佳成绩。

如果选择2,我们必须列出所有参与者按背部排序的数据 号码(他们注册的订单)

如果选择3,我们必须列出2012年之前订购的所有参与者数据 标记,从大到小。

处理完每个选项后,必须再次显示主菜单, 直到选择选项4,退出程序。

我坚持按标记订购数据,这是我的程序,请问有什么想法吗?

package prc;

import epsa.Cio;

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

public class Rrc2

{

    public static void main(String[] args)


    {

        int n=0;
        Tupla participants[]= new Tupla[15];
        while(true){    

        Cio.println("Choose what you would like to do");
        Cio.println("Type 1 to:Register a participant");
        Cio.println("Type 2 to:List all the participant's data");
        Cio.println("Type 3 to:List all the participants data by mark");
        Cio.println("Type 4 to:Quit");
    int i;

    i = Cio.readInt();

switch(i)
{
case 1:

    participants[n]= new Tupla();
    Cio.println("Insert dorsal number");
    participants[n].number = Cio.readInt();
    Cio.println("Insert participants name");
    participants[n].name = Cio.readString();
    Cio.println("Insert participants best mark for the year 2012");
    participants[n].bestMark2012 = Cio.readDouble(); 
    Cio.println("Insert participants best mark for the year 2011");
    participants[n].bestMark2011 = Cio.readDouble();
    Cio.println("Insert participants best mark for the year 2010");
    participants[n].bestMark2010 = Cio.readDouble();
    n++;



    break;

case 2:
    int t;

    for(t=0;t<15&&t<n;t++){ 

        Cio.println("name: " + participants[t].name);
        Cio.println("Dorsal number: " + participants[t].number);
        Cio.println("Best Mark 2012: " + participants[t].bestMark2012);
        Cio.println("Best Mark 2011: " + participants[t].bestMark2011);
        Cio.println("Best Mark 2010: " + participants[t].bestMark2010);
        Cio.println("//////////////////////////////////////////////////");
    }



    break;

case 3:

Collections.sort(participants);


break;

case 4:System.exit(0); 
break; 


default: 
    Cio.println("Not valid");

    break;


}
Cio.println("Press enter to continue");
Cio.readString();
}

    }
}

1 个答案:

答案 0 :(得分:0)

您只需要实施java.util.Comparator<Tupla>并将其传递给Collections.sort()(并使用java.util.List代替数组)

Collections.sort(participants, new TuplaComparator());

您需要为TuplaComparator实现(自己编写课程)。它应该实现接口java.util.Comparator<Tupla>。 (见javadocs。)

变量participants应该是ArrayList,而不是Collections.sort()的简单数组。

这有很多线索可供选择。我不会在我的答案中重现你的整个(作业?)程序代码。

相关问题