我这里有一个程序,它应该以长度为 1 的数组开始,允许用户在数组中输入一个条目,然后用户输入的每个条目都将数组的大小加倍以避免java.lang.ArrayIndexOutOfBoundsException错误。按照下面的代码编写方式,如果在第二个用户输入之后,计算机会跳过 else并直接输入“抱歉,数据库已满” 。如果我在第一个else if块中添加newRecords = records ,我会收到java.lang.ArrayIndexOutOfBoundsException错误。
public class PhoneDirectory5 {
public static void main(String args[]) {
**PhoneRecord[] records= new PhoneRecord[1];
int numRecords = 0;**
// Display list of commands
System.out.println("Phone directory commands: \n" +
" a - Add a new phone number\n" +
" f - Find a new phone number\n" +
" q - Quit\n" +
" d - Delete record\n");
// Read and execute commands
while (true) {
// Prompt user to enter a command
SimpleIO.prompt("Enter command (a, f, d, or q): ");
String command = SimpleIO.readLine().trim();
// Determine whether command is "a", "f", "q", or
// illegal. Execute command if illegal.
**if (command.equalsIgnoreCase("a"))** {
// Command is "a". prompt user for name and number,
// then create a phone record and store it in the
// database.
**if (numRecords < records.length) {
SimpleIO.prompt("Enter a new name: ");
String name = SimpleIO.readLine().trim();
SimpleIO.prompt("Enter new phone number: ");
String number = SimpleIO.readLine().trim();
records[numRecords] =
new PhoneRecord(name, number);
numRecords++;
} else if (numRecords == records.length) {
PhoneRecord[] newRecords = new PhoneRecord[records.length*2];
System.arraycopy(records, 0, newRecords, 0, records.length);
SimpleIO.prompt("Enter a new name: ");
String name = SimpleIO.readLine().trim();
SimpleIO.prompt("Enter new phone number: ");
String number = SimpleIO.readLine().trim();
newRecords[numRecords] =
new PhoneRecord(name, number);
numRecords++;**
} else
System.out.println("Sorry, database is full.");
} else if (command.equalsIgnoreCase("f")) {
// Command is "f". Prompt user for search key.
// Search the database for records whose name begins
// with the search key. Print these names and the
// corresponding phone numbers.
SimpleIO.prompt("Enter name to look up: ");
String key = SimpleIO.readLine().trim().toLowerCase();
for (int i = 0; i < numRecords; i++) {
String name = records[i].getName().toLowerCase();
if (name.startsWith(key)) {
System.out.println(records[i].getName() + " " +
records[i].getNumber());
break;
} else if (i == numRecords - 1)
System.out.println("Sorry, your search did not" +
" match any records.");
}
} else if (command.equalsIgnoreCase("d")) {
SimpleIO.prompt("Enter the name of the record to delete: ");
String key = SimpleIO.readLine().trim().toLowerCase();
for (int i = 0; i < numRecords; i++) {
String name = records[i].getName().toLowerCase();
if (name.startsWith(key)) {
records[i] = new PhoneRecord("", "");
break;
} else if (i == numRecords - 1)
System.out.println("Sorry, your search did not match" +
" any records.");
}
} else if (command.equalsIgnoreCase("q")) {
// Command is "q".. Terminate program
System.out.println("You have elected to exit the phone directory.");
return;
} else {
// Command is illegal. Display error message.
System.out.println("Command was not recognized; " +
"please enter only a, f, d or q.");
}
System.out.println();
}
}
}
// Represents a record containing a name and a phone number
class PhoneRecord {
private String name;
private String number;
// Constructor
public PhoneRecord(String personName, String phoneNumber) {
name = personName;
number = phoneNumber;
}
// Returns the name stored in the record
public String getName() {
return name;
}
// Returns the phone number stored in the record
public String getNumber() {
return number;
}
}
话虽如此,当我以这种方式分配新的数组空间时......
else if (numRecords == records.length) {
PhoneRecord[] newRecords = new PhoneRecord[records.length*2];
for (int i = 0; i < records.length; i++)
newRecords[i] = records[i];
records = newRecords;
SimpleIO.prompt("Enter a new name: ");
String name = SimpleIO.readLine().trim();
SimpleIO.prompt("Enter new phone number: ");
String number = SimpleIO.readLine().trim();
newRecords[numRecords] =
new PhoneRecord(name, number);
numRecords++;
...该程序完全按照我的需要执行,即,将每个用户条目的数组大小加倍,并且从不打印“对不起,但数据库已满”消息。我的问题是,为什么我不能让程序使用.arraycopy方法?任何帮助将非常感激。
当我这样做...
else if (numRecords == records.length) {
PhoneRecord[] newRecords = new PhoneRecord[records.length*2];
System.arraycopy(records, 0, newRecords, 0, records.length);
**newRecords = records;**
SimpleIO.prompt("Enter a new name: ");
String name = SimpleIO.readLine().trim();
SimpleIO.prompt("Enter new phone number: ");
String number = SimpleIO.readLine().trim();
newRecords[numRecords] =
new PhoneRecord(name, number);
numRecords++;
...是当我收到 arrayindexoutofbounds 错误时。