数组排序使用变量在数组 [英] Sorting Arrays Using Variables Within the Array

查看:136
本文介绍了数组排序使用变量在数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我道歉我的Java的薄弱知识和术语。我将尽我所能。我想下面的数组进行排序, eventStuff ,由存储在阵列中的变量。我给用户三个选项,事件号(字母,数字×3)进行选择,输入的客人的数量(5至100,其中包括),以及事件类型(1至4,其中包括)。我目前苦难如何做到这一点了完整的心理障碍。有我设置的阵列不正确,或者有更简单的方式做到这一点使用的是什么,我这么远?谢谢你的帮助!

更新:所以,我已经得到了所有的数字排序的句柄,但字母(例如A111)事件codeSTRING 我是无法理解。我检查了一些论坛和应用许多合理的解决办法,但它们是无效或抛出一个错误。我不确定我应该做的,在这一点上。

 第九章包;
进口Chapter9.Event;
进口javax.swing.JOptionPane中;
进口java.util.Arrays中;
公共类EventDemo
{    公共静态无效的主要(字串[] args)
    {
        callMotto();
                INT X;
                事件[] = eventStuff新事件[8];
                为(X = 0; X&下; 8; ++ x)的
                {
                    eventStuff [X] =新的事件();
                    eventStuff [X] .setEvent codeString的();
                    eventStuff [X] .setGuests();
                    eventStuff [X] .setContactNumber();
                    eventStuff [X] .setEventStr();
                }
                //排序方法开始
                    字符串排序;
                    INT sortMethod;                    排序= JOptionPane.showInputDialog(请选择要保留排序方法:\\ n
                            +1由事件号\\ n排序
                            +2对客人数量排序的\\ n
                            +3按事件类型排序);
                    sortMethod =的Integer.parseInt(排序);
                    //事件数排序开始
                    如果(sortMethod == 1)
                    {                    }
                    //事件数排序结束
                    //事件排序客人开始
                    如果(sortMethod == 2)
                    {                    }
                    //事件排序客人结束
                    //事件类型分类开始
                    如果(sortMethod == 3)
                    {                    }
                    //事件类型排序结束
                //排序方法结束
    }    公共静态无效callMotto()
    {        JOptionPane.showMessageDialog(NULL,************ ************ \\ n
                        +*卡莉的使,使党的食物!* \\ n
                        +************ *******);    }    }


解决方案

例如,如果要排序的阵列上的客人数量,你可以使用这样的事情:

  Arrays.sort(eventStuff,新的比较<事件>(){        @覆盖
        公众诠释比较(事件O1,O2事件){            如果(o1.getGuests()&所述; o2.getGuests())
                返回-1;
            否则如果(o1.getGuests()== o2.getGuests())
                返回0;
            其他
                返回1;        }
    });

我想, getGuests 返回客人的数量。

Firstly, my apologies for my weak knowledge of Java and its terminology. I'll do my best. I'm trying to sort the array below, eventStuff, by the variables stored within the array. I am giving the user three options to choose from, an event number (Letter, number x 3), the number of guests entered (between 5 and 100, including), and an event type (1 to 4, including). I am currently suffering a complete mental block on how to do this. Have I set up the array incorrectly, or is there an easier way to do this using what I've got so far? Thank you for the assistance!

Update: So I've got a handle on all the numeric sorting, but the alphanumeric (e.g. A111) eventCodeString is beyond me. I have checked several forums and applied many reasonable solutions, but they are either ineffective or throw an error. I am unsure of what I should do, at this point.

package Chapter9;
import Chapter9.Event;
import javax.swing.JOptionPane;
import java.util.Arrays;
public class EventDemo
{

    public static void main(String[] args)
    {
        callMotto();
                int x;
                Event[] eventStuff = new Event[8];
                for(x = 0; x < 8; ++x)
                {
                    eventStuff[x] = new Event();
                    eventStuff[x].setEventCodeString();
                    eventStuff[x].setGuests();
                    eventStuff[x].setContactNumber();
                    eventStuff[x].setEventStr();
                }
                //Sorting method start
                    String sorting;
                    int sortMethod;

                    sorting = JOptionPane.showInputDialog("Please chooose sorting method:\n"
                            + "1 to sort by event number\n"
                            + "2 to sort by number of guests\n"
                            + "3 to sort by event type");
                    sortMethod = Integer.parseInt(sorting);
                    //Event number sorting start
                    if(sortMethod == 1)
                    {

                    }
                    //Event number sorting end
                    //Event guest sorting Start
                    if(sortMethod == 2)
                    {

                    }
                    //Event guest sorting end
                    //Event type sorting start
                    if(sortMethod == 3)
                    {

                    }
                    //Event type sorting end
                //Sorting method end
    }

    public static void callMotto()
    {

        JOptionPane.showMessageDialog(null, "*******************************************************\n"
                        +   "* Carly's Makes The Food That Makes The Party! *\n"
                        +   "*******************************************************");

    }

    }   

解决方案

For example, if you want to sort your array over number of guests, you can use something like that:

    Arrays.sort(eventStuff, new Comparator<Event>() {

        @Override
        public int compare(Event o1, Event o2) {

            if (o1.getGuests() < o2.getGuests())
                return -1;
            else if (o1.getGuests() == o2.getGuests())
                return 0;
            else
                return 1;

        }                                   
    });

I suppose that getGuests returns the number of guests.

这篇关于数组排序使用变量在数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆