gpt4 book ai didi

Java 数组搜索和排序未按预期工作

转载 作者:行者123 更新时间:2023-12-02 10:02:05 27 4
gpt4 key购买 nike

我目前正在学习 Java 简介类(class),其中我们目前正在讨论数组主题。我们有一个类实验室,假设创建一个由两个类(Passenger.java 和 Demo.java)组成的简单数组程序。该数组可以是任意大小的(最小 4),我们可以将详细信息硬编码到几个元素中。我们应该创建两个按 ID 和姓名(名字 + 姓氏)搜索的方法,以及两个按 ID 和姓氏升序排序的方法(冒泡和插入)。

在 ID 和名称的搜索方法中,当我输入数组中不存在的 ID 时,出现 java.lang.NullPointerException 错误。如果没有找到,它应该返回我编写的错误消息。

在我的 ID 排序方法中,我还在第二个循环后收到 java.lang.NullPointerException 错误。对于我的姓氏排序方法,我不知道如何实现。

因此我可能知道如何使用完整的代码示例来解决这两个问题。

注意:

Passenger.java

import java.util.Scanner;

public class Passenger {

private String title;
private String firstName;
private String lastName;
private long id;

public Passenger() {

}

public Passenger(String title, String firstName, String lastName, long id) {
this.title = title;
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
}

public void enterDetails() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your title: ");
title = keyboard.next();
System.out.print("Enter your first name: ");
firstName = keyboard.next();
System.out.print("Enter your last name: ");
lastName = keyboard.next();
System.out.print("Enter your ID: ");
id = keyboard.nextLong();
}

public void outputDetails() {
System.out.print();
System.out.print("Passenger name: " + title + " " + firstName + " " + lastName);
System.out.print("Identify Number: " + id);
System.out.print();
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public long getId() {
return id;
}

}

演示.java

import java.util.Scanner;

public class Demo {

public static Passenger[] passengers = new Passenger[10];
public static Scanner kb = new Scanner(System.in);

public static void main(String[] args) {

int option = 0;

passengers[0] = new Passenger("Mr", "Benjamin", "Parker", 12345678);
passengers[1] = new Passenger("Mrs", "Mary", "Long", 96421368);
passengers[2] = new Passenger(....);
passengers[3] = new Passenger(....);
passengers[4] = new Passenger(....);
passengers[5] = new Passenger(....);
passengers[6] = new Passenger(....);
passengers[7] = new Passenger(....);

while(option != 7) {
System.out.println("Please select an option:"
+ "\n1) Enter passenger particulars to array"
+ "\n2) Display passenger particulars from array"
+ "\n3) Search passenger particulars using ID"
+ "\n4) Search passenger particulars using both surname and name"
+ "\n5) Display passenger particulars in ascending order of ID"
+ "\n6) Display passenger particulars in ascending order of surname"
+ "\n7) Quit
)
option = kb.nextInt();

switch(option) {
case 1:
inputDetails();
break;
case 2:
outputDetails();
break;
case 3:
searchDetailsID();
break;
case 4:
searchDetailsName();
break;
case 5:
sortDetailsID();
break;
case 6:
sortDetailsName();
break;
case 7:
System.out.println("You quitted the program!");
break;
default:
System.out.println("Invalid input!");
break;
}
}
}

public static void inputDetails() {
int element = 0;
for (int i = 0; i < passengers.length; i++) {
if(passengers[i] != null)
element++;
}
passengers[element] = new Passenger();
passengers[element].enterDetails();
element++;
}

public static void outputDetails() {
for(int i = 0; i < passengers.length; i++) {
passengers[i].outputDetails();
}
}

public static void searchDetailsID() {
boolean isFound = false;
System.out.print("Please enter an passenger id: ");
long id = kb.nextLong();

for(int i = 0; i < passengers.length; i++) {
if(id == passengers[i].getid()) {
passengers[i].outputDetails();
isFound = true;
break;
}
}

if(!isFound) {
System.out.println("No passenger with that id!");
}
}

public static void searchDetailsName() {
boolean isFound = false;
System.out.print("Please enter an passenger first name: ");
String firstName = kb.next();
System.out.print("Please enter an passenger last name: ");
String lastName = kb.next();

for(int i = 0; i < passengers.length; i++) {
if(firstName.equalsIgnoreCase(passengers[i].getFirstName()) && lastName.equalsIgnoreCase(passengers[i].getLastName())) {
passengers[i].outputDetails();
isFound = true;
break;
}
}

if(!isFound) {
System.out.println("No passenger with that name!");
}
}

public static void sortDetailsID() {
long temp = 0;
for(int i = 0; i < passengers.length; i++) {
for(int j = 0; j < passengers.length - 1; j++) {
if(passengers[i].getId() > passengers[j].getId()) {
temp = passengers[i].getId();
passengers[i] = passengers[j];
}
}
}
}

public static void sortDetailsName() {

}

}

最佳答案

您的 NPE 堆栈跟踪是:

Exception in thread "main" java.lang.NullPointerException
at com.Demo.searchDetailsID(Demo.java:90)
at com.Demo.main(Demo.java:46)

如果你深入评估你的代码,你会发现你初始化了 passengers具有 10 的数组,但您仅在 main 中实例化 8 索引方法Demo类(class)。换句话说,当循环想要从数组中读取 910 索引时,会抛出 NPE。因此,如果您替换 Demo 的第一行类包含以下行,您的代码将正常工作:

public static Passenger[] passengers = new Passenger[8];

解决方案 2:

当然,您可以使用 if ,如下所示:

public static void searchDetailsID() {
boolean isFound = false;
System.out.print("Please enter an passenger id: ");
long id = kb.nextLong();

for(int i = 0; i < passengers.length; i++) {
if(passengers[i] != null /* I append this check*/ && id == passengers[i].getId()) {
passengers[i].outputDetails();
isFound = true;
break;
}
}

if(!isFound) {
System.out.println("No passenger with that id!");
}
}

关于Java 数组搜索和排序未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55549062/

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