gpt4 book ai didi

java - 避免数组中出现 null?

转载 作者:行者123 更新时间:2023-11-30 05:56:26 25 4
gpt4 key购买 nike

我是一名新手编码员,我们在大学里接到的任务是只使用数组(我问老师,说没有数组列表什么的,想粗略地做一下)

它是关于创建一个数组,您可以在其中插入、搜索或删除值。我通过搜索和应用解决方案弄清楚了大部分内容。

但是他们想要一个输出,这样如果我删除然后我搜索该值,它会显示该值消失了,但问题是因为该值被删除,Java 在那里放置了一个 null,所以当 for 循环循环时通过所有空值,它会产生可怕的 NullPointerException 错误。我目前正在寻找具有这些限制的解决方案,但没有结果,而且我的 Java 词汇和术语目前确实很短:P

import static java.lang.System.out;
import java.util.Scanner;

public class JavaApplication
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);

//initialize String array x20
String[] regName = new String[20];
int regCount = 0;
int func = 0;
while (func == 0) //Main Menu Looper
{
out.println("Select function by entering its number.");
out.println("[1] Insert");
out.println("[2] Search");
out.println("[3] Delete");
out.println("[4] Exit");
out.print("Choose Operation: ");
func = kb.nextInt(); //Choose Option
out.print("======================================");
out.print("\n");

switch (func)
{
case 1: //Insertion
//set Array index start
char yesNo;
do
{
//Inserting into arrays loop
out.print("Insert student last name: ");
regName[regCount] = kb.next();
regCount++;
out.print("\n");

//Viewing loop
out.println("Student List: ");
for (int ctrl = 0; ctrl < regCount; ctrl++)
{
out.println(regName[ctrl]);
}
out.print("\n");

//Question loop
out.print("You want to insert again(Y/N):");
yesNo = kb.findWithinHorizon(".", 0).charAt(0);
if (yesNo == 'y' || yesNo == 'Y')
{
yesNo = 'y';
}
} while (yesNo == 'y');
func = 0;
break;

case 2: //Searching
out.print("Enter keyword: ");
String search = kb.next();
boolean found = false;
int searchCount = 0;
for (int ctrl = 0; ctrl < regCount; ctrl++)
{
if (regName[ctrl].equalsIgnoreCase(search)) {
found = true;
out.println(search + " has " + " a match.");
}
else
{
out.println(search + " has " + " not found.");
}
}
out.print("\n");
func = 0;
break;

case 3: //Deleting
out.print("type surname you want to delete: ");
String toDelete = kb.next();
for (int ctrl = 0; ctrl < regCount; ctrl++)
{
if (regName[ctrl].equalsIgnoreCase(toDelete)) {
regName[ctrl] = null;
out.println("Record deleted.");
}
}
out.print("\n");
func = 0;
break;

} //switch
} //while
} //main
} //class

最佳答案

其他答案建议检查 null。但这并不能解决你的问题。因为您的代码的其余部分预计您的学生列表中不会出现空白。

删除其中一些名称后尝试更改名称:

case 3: //Deleting
out.print("type surname you want to delete: ");
String toDelete = kb.next();
int deleted = 0;
for (int ctrl = 0; ctrl < regCount; ctrl++) {
if (regName[ctrl].equalsIgnoreCase(toDelete)) {
out.println("Record deleted.");
deleted++;
}
if(deleted > 0) {
int newCtrl = ctrl + deleted;
regName[ctrl] = (newCtrl < regCount) ? regName[newCtrl] : null;
}
}
regCount -= deleted;
out.print("\n");
func = 0;
break;

此解决方案假设您的应用程序允许重复条目。

我还发现你的search操作打印<Name> has not found即使有匹配也多次。尝试像这样更改它:

case 2: //Searching
out.print("Enter keyword: ");
String search = kb.next();
boolean found = false;
int searchCount = 0;
for (int ctrl = 0; ctrl < regCount; ctrl++) {
if (regName[ctrl].equalsIgnoreCase(search)) {
found = true;
out.println(search + " has a match : #" + ctrl);
break;
}
}
if(!found) {
out.println(search + " has not found.");
}
out.print("\n");
func = 0;
break;

更新:仅删除第一次出现

case 3: //Deleting
out.print("type surname you want to delete: ");
String toDelete = kb.next();
int deletedIndex = -1;
for (int ctrl = 0; ctrl < regCount; ctrl++) {
if(deletedIndex >= 0) {
int newCtrl = ctrl + 1;
regName[ctrl] = (newCtrl < regCount) ? regName[newCtrl] : null;
} else if (regName[ctrl].equalsIgnoreCase(toDelete)) {
deletedIndex = ctrl;
out.println("Record deleted : #" + deletedIndex);
regCount--;
}
}
out.print("\n");
func = 0;
break;

关于java - 避免数组中出现 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53094352/

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