我一直想学习如何在数组中添加或删除元素。我知道在这些类型的东西上使用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;
}
}
答案 0 :(得分:0)
只要您输入mode()
方法,就可以重新初始化names
数组。这就是为什么它只包含null
值,因此NullPointerException
。你应该把它变成班上的一个领域。
此外,按设计,其长度始终为2。
答案 1 :(得分:0)
ArrayList在功能上与您正在寻找的完全相同,而我将要提供的替代方案将完全以低效或有吸引力的方式完成ArrayList的工作。作为免责声明,我要说你应该学习使用ArrayList,但我会按你的条件回答你的问题,你会同时学习一下ArrayList的工作原理。
要调整数组大小,无论如何都必须创建一个新数组。最有效的方法是使用临时数组来存储数据,然后像这样重新实例化主数组变量
// Inside your adjustable array class. Let's call it "AdvArray"
// A field that's instantiated inside the constructor
int[] exampleArray = {1, 2, 3, 4, 5}; // create and fill an int array
public void addElement(int element){
//Make temp array the size of your data and fill it
int[] tempArray = new int[exampleArray.length];
for(int i = 0; i<exampleArray.length; i++){
tempArray[i] = exampleArray[i];
}
//reinstantiate exampleArray and fill with old data
exampleArray = new int[exampleArray.length+1];
for(int i = 0; i<tempArray.length; i++){
exampleArray[i] = tempArray[i];
}
//Add new element to larger array
exampleArray[exampleArray.length-1] = element;
}// ===================================================
// Example usage
exampleAdvArray.addElement(500);
此方法将一个添加到最后,但您可以通过创建带有要添加的索引的溢出来跟随其想法,然后反转两个进程以获取remove方法。然后你需要做的就是为任何给定索引的元素添加一个getter,或者甚至是一个返回“exampleArray”字段的getArray方法。
或者,您的代码如下所示:
ArrayList<int> exampleList = new ArrayList<int>();
for(int i=1; i<=5; i++){
exampleList.add(i);
}
exampleList.add(500);