2015-06-16 41 views
1

我創建「數據庫」與的ArrayList對象的數組來代替ArrayList的,但我需要陣列對象來代替它。所以我該怎麼辦的變化,而不破壞這個班?需要與

import java.util.*; 

// ArrayList to store Users. 
public class Appointment { 
    public static void main(String[] args) { 
     String command = ""; 
     String date = ""; 
     String appointment = ""; 
     String time = ""; 
     int id = 0; 
     ArrayList<User> data = new ArrayList<User>(); 
     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:"); 
       day = input.nextLine(); 
       System.out.print("Appointment: "); 
       appointment = input.nextLine(); 

       User validUser = new User(date, time, appointment); 
       data.add(validUser); 
       System.out.println("Successful!"); 
       System.out.println(""); //Adds an appointment to database if it's valid. 
      } 
     } 
    } 
+2

爲什麼你不能在你的ArrayList上調用.toArray()? – Shar1er80

+1

你爲什麼要降級?每個人都從陣列升級到列表, 使用ArrayList更好的方法 – Vihar

+0

@Vihar這是不正確的。 ArrayLists和Arrays有不同的用途,這兩個數據結構之間沒有「升級」與「降級」。 – neuronaut

回答

0

要將ArrayList轉換爲該類型的數組,請使用.toArray()。在這種情況下,它將是

User [] users = data.toArray(new User [0]);

+0

好找@Soham。我應該更快地搜索重複內容,而不僅僅是回答問題。 – JavaOG

0

實現使用數組的唯一方法是在使用新的「添加」命令時動態擴展數組。

因此,你需要這種方法來更新和擴展你的數組;

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 

希望這可以幫助你。