實現使用數組的唯一方法是在使用新的「添加」命令時動態擴展數組。
因此,你需要這種方法來更新和擴展你的數組;
public static User[] add(User[] userArray, User user) {
//null array check
if(userArray == null) {
userArray = new User[1];
userArray[0] = user;
return userArray;
}
User[] newArray = new User[userArray.length+1];
int i = 0;
for(; i < userArray.length; i++)
newArray[i] = userArray[i];
newArray[i] = user; //already incremented with post-increment operator
return newArray;
}
而整個測試代碼如下;
import java.util.*;
// ArrayList to store Users.
public class TestAppointment {
public static void main(String[] args) {
String command = "";
String date = "";
String appointment = "";
String time = "";
int id = 0;
User[] data = null;
Scanner input = new Scanner(System.in);
while (!command.equalsIgnoreCase("Exit")) {
System.out.println("Please Enter a Command or Type Exit: ");
command = input.nextLine();
if (command.equalsIgnoreCase("Add")) {
System.out.print("Data: ");
date = input.nextLine();
System.out.print("Time: ");
time = input.nextLine();
// System.out.println("Day of appointment:");
// date = input.nextLine();
System.out.print("Appointment: ");
appointment = input.nextLine();
User validUser = new User(date, time, appointment);
//data.add(validUser); // we dont need this anymore
data = add(data, validUser);
System.out.println("Successful!");
System.out.println(""); // Adds an appointment to database if
// it's valid.
}
}
System.out.println(data.length);
for(int i = 0; i < data.length; i++) {
System.out.printf("%d: %s:%s:%s\n",i,data[i].getDate(),data[i].getTime(),data[i].getAppointment());
}
}
public static User[] add(User[] userArray, User user) {
//null array check
if(userArray == null) {
userArray = new User[1];
userArray[0] = user;
return userArray;
}
User[] newArray = new User[userArray.length+1];
int i = 0;
for(; i < userArray.length; i++)
newArray[i] = userArray[i];
newArray[i] = user; //already incremented with post-increment operator
return newArray;
}
private static class User {
private String date;
private String time;
private String appointment;
public User(String date, String time, String appointment) {
this.date = date;
this.time = time;
this.appointment = appointment;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getAppointment() {
return appointment;
}
public void setAppointment(String appointment) {
this.appointment = appointment;
}
}
}
該測試代碼的輸出如下;
Please Enter a Command or Type Exit:
add
Data: testData1
Time: testTime1
Appointment: testApp1
Successful!
Please Enter a Command or Type Exit:
add
Data: testData2
Time: testTime2
Appointment: testApp2
Successful!
Please Enter a Command or Type Exit:
exit
2
0: testData1:testTime1:testApp1
1: testData2:testTime2:testApp2
希望這可以幫助你。
爲什麼你不能在你的ArrayList上調用.toArray()? – Shar1er80
你爲什麼要降級?每個人都從陣列升級到列表, 使用ArrayList更好的方法 – Vihar
@Vihar這是不正確的。 ArrayLists和Arrays有不同的用途,這兩個數據結構之間沒有「升級」與「降級」。 – neuronaut