gpt4 book ai didi

Java indexOfMaxInRange

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

<分区>

我正在为我的 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);
}
}
}

public class Reservations {

protected String name;
protected String phone;
protected int numberInAParty;
protected String time;


public Reservations() {
this.name = "";
this.phone = "";
this.time = "";
this.numberInAParty = 0;
}

public Reservations(String name, String phone, String time, int numberInAParty, int size) {
this.name = name;
this.phone = phone;
this.time = time;
this.numberInAParty = numberInAParty;
}

public void printReservation (Reservations x) {
System.out.println("Name: " + x.name);
System.out.println("Phone number: " + x.phone);
System.out.println("Time: " + x.time);
System.out.println("Party number: " + x.numberInAParty);
}

}

列表是这样初始化的

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,它只是为了调试目的。但是,如果我将 max 替换为 0,它会起作用,但我不明白为什么。如果您还需要什么,请告诉我。

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

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

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

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