【发布时间】:2015-10-19 01:46:18
【问题描述】:
我一直想学习如何在数组中添加或删除元素。我知道在这些类型的东西上使用 Arraylist 包更容易,但我想学习如何在不使用它的情况下添加或删除元素。我曾尝试为此创建代码,但最终出现了一些错误。
我的代码的输出应该是这样的:
********************^
1 - Search Name
2 - Add Name
3 - Delete Name
Enter mode:2
Enter new name:professor x
Do you want to add a name again?(y/n):y
Enter new name:professor y
Do you want to add a name again?(y/n):n
********************^
1 - Search Name
2 - Add Name
3 - Delete Name
Enter mode: 1
Enter name:professor x
Match found!
但在搜索以前输入的记录时,我的代码给了我一个null pointer exception:
Exception in thread "main" java.lang.NullPointerException
at Practice.Practice1Main.sequentialSearch(Practice1Main.java:95)
at Practice.Practice1Main.mode(Practice1Main.java:18)
at Practice.Practice1Main.getMode(Practice1Main.java:79)
at Practice.Practice1Main.caseTwo(Practice1Main.java:63)
at Practice.Practice1Main.mode(Practice1Main.java:33)
at Practice.Practice1Main.getMode(Practice1Main.java:79)
at Practice.Practice1Main.main(Practice1Main.java:6)
我的代码:
package Practice;
import java.util.Scanner;
public class Practice1Main {
public static void main(String[]args){
getMode();
}
public static void mode(int mode){
Scanner hold = new Scanner(System.in);
int numInput = 2;
String[] names = new String[numInput];
String response = "";
if(mode == 1){
do{
int result = sequentialSearch(names, getNames());
if(result == -1){
System.out.print("No match found!\n");
}else{
System.out.print("Match found!\n");
}
System.out.print("Do you want to search again?(y/n):");
response = hold.nextLine();
if(response.equalsIgnoreCase("n")){
getMode();
}
}while(response.equalsIgnoreCase("y"));
}else if(mode == 2){
caseTwo(response, names, numInput);
}else if(mode == 3){
// TODO: Part 3
}else{
System.out.print("ERROR 350\n");
}
}
public static void caseTwo(String response, String[] names, int numInput){
Scanner hold = new Scanner(System.in);
do{
int x = 0;
System.out.print("Enter new name:");
names[x] = hold.nextLine();
System.out.print("Do you want to add a name again?(y/n):");
response = hold.nextLine();
if(response.equalsIgnoreCase("y")){
x++;
if(x == 2){
int add = numInput + 1;
names = new String[add];
System.out.print("Enter new name:");
names[add] = hold.nextLine();
System.out.print("Do you want to add a name again?(y/n):");
response = hold.nextLine();
if(response.equalsIgnoreCase("y")){
add++;
}
}
}else if(response.equalsIgnoreCase("n")){
getMode();
}else{
System.out.print("CASE 2 ERROR!\n");
}
}while(response.equalsIgnoreCase("y"));
}
public static void getMode(){
Scanner hold = new Scanner(System.in);
int mode;
System.out.print("********************\n");
System.out.print("1 - Search Name\n");
System.out.print("2 - Add Name\n");
System.out.print("3 - Delete Name\n");
System.out.print("Enter mode:");
mode = hold.nextInt();
mode(mode);
}
public static String getNames(){
Scanner hold = new Scanner(System.in);
String name;
System.out.print("Enter name:");
name = hold.nextLine();
return name;
}
public static int sequentialSearch(String[]names,String name){
int index = 0;
int element = -1;
boolean found = false;
while(!found && index < names.length){
if(names[index].equalsIgnoreCase(name)){
found = true;
element = index;
}
index++;
}
return element;
}
}
【问题讨论】:
-
你不能在java中调整数组的大小
-
您是否有理由不能使用 ArrayList 或者您只是不想使用?数组在声明时具有固定大小,因此您不能“添加”或“删除”,只能在索引处重新分配值。
-
你应该修正你的帖子标题,你不小心把所有的大写字母都放了。
-
我会按照其他人的建议使用数组列表。它更易于使用,并且可以按照您的意愿工作。