Java indexOfMaxInRange

Java indexOfMaxInRange,java,arrays,loops,Java,Arrays,Loops,我正在为我的Java类做一个项目,我不知道如何找到最大时间的索引,我需要帮助才能让这个方法工作 public class ReservationList { Reservations[] reservationsArray; String name, phone, time; int index, numberInAParty; public ReservationList(int size) { reservations

我正在为我的Java类做一个项目,我不知道如何找到最大时间的索引,我需要帮助才能让这个方法工作

public class ReservationList {
        Reservations[] reservationsArray;
        String name, phone, time;
        int index, numberInAParty;

    public ReservationList(int size) {
        reservationsArray = new Reservations[size];
    }

    //Adds items to the reservations array
    public void addArrayItem(int index, Reservations reservation) {        
        this.reservationsArray[index] = reservation;
    }

    //Finds the index of the highest number
    public static int indexOfMaxInRange (ReservationList list, int low, int high) {
        int timeZero = Integer.valueOf(list.reservationsArray[0].time.replace(":", ""));        
        int max = timeZero;        
        int maxIndex = 0;    
        for (int i = low; i < high; i++) {            
            int time = Integer.valueOf(list.reservationsArray[i].time.replace(":", ""));         
            if (time > max) {
                System.out.println("Pass");
                maxIndex = i;
                //max = Integer.valueOf(list.reservationsArray[i].time);
            }
        }
        return maxIndex;
    }

    //Swaps array elements
    private static void swapElement (ReservationList list, int indexOne, int indexTwo) {
        Reservations temp = list.reservationsArray[indexOne];
        list.reservationsArray[indexOne]= list.reservationsArray[indexTwo];
        list.reservationsArray[indexTwo]= temp;
    }

    //Sorts through the array
    protected static void sortArray(ReservationList list) {  
        for(int i = list.reservationsArray.length;i >= 0; i--){
            int big = indexOfMaxInRange(list,0,i);
            swapElement(list, big,i);
        }
        for(int i=list.reservationsArray.length;i>0;i=i-1){
            swapElement(list,i,i-1);
        }   
    }
}

下面是列表的初始化方式

private void loadArray (String fileName, ReservationList list) throws IOException, FileNotFoundException {
    BufferedReader reader = new BufferedReader (new InputStreamReader(new FileInputStream(fileName)));     
    Reservations temp = new Reservations();
    String reservation;

    int i = 0;
    String delimiters = "[ ]+";        

    try {                        
        while ((reservation = reader.readLine()) !=null) {
            String[] splitReservation = reservation.split(delimiters);
            temp.name = splitReservation[0];
            temp.phone = splitReservation[1];
            temp.numberInAParty = Integer.valueOf((String)splitReservation[2]);
            temp.time = splitReservation[3];

            list.addArrayItem(i, temp);
            list.sortArray(list);
            ReservationsTextArea.append(list.reservationsArray[i].name + "  " + list.reservationsArray[i].phone + "  " + list.reservationsArray[i].numberInAParty + "  " + list.reservationsArray[i].time + "\n");                                                
            i++;
        }


    } catch (NumberFormatException e) {
        System.out.println(e.getMessage());
    }                        
}
如果您需要更多的代码,请告诉我。这是我第一次发帖,所以如果我做错了什么,请告诉我

我正在输入一个包含姓名、电话号码、聚会金额和时间的对象。这种方法应该对时间进行排序,以便最小时间是第一个,最晚时间是最后一个

我在打印出pass后得到一个nullpointerexception,它只是为了调试的目的。如果我用0替换max,它就可以工作了,但是我不明白为什么。如果你还需要什么,请告诉我

我没有创造这些方法中的一些,所以请不要批评任何方法。我只想让它修复指定的内容。抱歉,它太长了。我们小组使用NetBeans中的swing编辑器来节省时间,请不要评论我将如何不学习任何东西等。我知道

添加对象的方法是loadArray,DisplayAllButtonActionPerformed调用该方法


我也知道sortArray在一个循环中,在它工作之前,我只是将它保留在那里。

更改loadArray代码,以便在循环的每个迭代中创建一个新的临时对象

while ((reservation = reader.readLine()) !=null) {
   Reservations temp = new Reservations();
   temp.name = splitReservation[0];
   // etc

否则,您总是在同一个对象上工作

我无法缩小NullPointerException的范围,因此您可能需要调试正在读取的文件,并在读取所有数据后检查数组

然而,到目前为止我发现的问题是,
sortArray
是错误的。初始化
i=list.length
,这将引发
索引自动边界异常
。出于某种原因,您也有两次相同的for循环

这里是修复方法(我已经验证过)

至于最大指数法。你必须在某个地方得到一个空对象才能得到那个错误。从我的简单测试来看,这对我很有效

public static int indexOfMaxInRange(ReservationList list, int low, int high) {
    int max = Integer.MIN_VALUE; // Set this to be the lowest possible integer
    int maxIndex = low;

    for (int i = low; i < high; i++) {
        String timestamp = list.reservationsArray[i].time.replace(":", "");
        int time = Integer.valueOf(timestamp);

        if (time > max) {
            maxIndex = i;
            max = time;
        }
    }
    return maxIndex;  // note if low >= high, low is returned
}

提供程序试图生成的示例输入和输出会有很大帮助。如果您有一个list类,它包含带有时间字段的某种类型的
保留
对象数组?你能加上所有相关的课程吗?
ReservationList
类本身的用途是什么?预期的结果是什么?现在您已经添加了类。对对象建模仍然有点困惑。为什么
ReservationList
本身有一个名称和时间以及
Reservations
的所有其他字段?您还忘了在列表中实际插入保留对象的位置。预期的结果是,它会对数组中的时间进行排序,使6:30低于8:30等等。这并不能解决问题,但感谢您的建议。@Sandpiper131-不过您还是应该这样做。否则列表中的每个对象都有相同的信息。不,它没有,因为它从一个单独的文件中读取。别担心,它会改变。我确实这么做了,但它并没有改变程序运行的方式。抱歉,如果我只是愚蠢。使用调试器,它将显示所有对象都是相同的对象。顺便说一句,您可以大大简化您的代码,只需使用Map@Sandpiper131-很抱歉,此代码没有帮助您,但这并没有赋予您完全使我为帮助您所做的工作无效的权利。我重新创建了项目的所有必要部分,并使所有这些方法都能正常工作。如果您确实想学习如何编写代码,请学习使用调试器遍历代码。甚至忘记GUI,像我一样构建更小的模块,以确保逻辑正常工作。
protected static void sortArray(ReservationList list) {
    for (int i = list.reservationsArray.length - 1; i >= 0; i--) {
        int big = indexOfMaxInRange(list, 0, i);
        swapElement(list, big, i);
    }
}
public static int indexOfMaxInRange(ReservationList list, int low, int high) {
    int max = Integer.MIN_VALUE; // Set this to be the lowest possible integer
    int maxIndex = low;

    for (int i = low; i < high; i++) {
        String timestamp = list.reservationsArray[i].time.replace(":", "");
        int time = Integer.valueOf(timestamp);

        if (time > max) {
            maxIndex = i;
            max = time;
        }
    }
    return maxIndex;  // note if low >= high, low is returned
}
 try {                        
    while ((reservation = reader.readLine()) != null) {            
        String[] splitReservation = reservation.split("\\s+"); // this is the proper delimiter you want

        Reservations temp = new Reservations();
        temp.name = splitReservation[0];
        temp.phone = splitReservation[1];
        temp.numberInAParty = Integer.valueOf(splitReservation[2]);
        temp.time = splitReservation[3];

        list.addArrayItem(i, temp);                                      
        i++;
    }
} catch (NumberFormatException e) {
    System.out.println(e.getMessage());
}     
ReservationList.sortArray(list); // this is a static method, so treat it like one