2013-03-05 64 views
0

我試圖自學Java,並開始瀏覽BlueJ的例子。簡單的Java Array新手

在手術系統的情況下,我需要將患者及其地址添加到陣列,然後才能添加患者,然後列出患者。

該示例使用向量和硬編碼向向量添加名稱,但是我想使用數組並允許用戶向數組添加名稱和地址。

任何人都可以提供一些指導?

我有以下但不知道從哪裏去。

public class Patient 
{ 
    public String name; 
    public String address; 

    public Patient(String n, String a) { 
     name = n; 
     address = a; 
    } 


} 
+1

也許你應該從[Java教程](http://docs.oracle.com/javase/tutorial/java/TOC.html)開始。我建議從頭開始,但也有[數組]的一節(http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html)。 – 2013-03-05 13:39:56

+1

值得注意的是,在這裏使用'Vector'可能是正確的選擇。在Java中(和許多語言一樣),因爲數組維度是固定的,所以對數組進行生長是比較冗長的(以允許添加更多元素)。 'Vector's(以及其他Java標準集合類,如'List's和'Set's)在這裏更加有效。 – RudolphEst 2013-03-05 13:50:39

+1

也許值得一提的是,如果你已經有了使用'Vector'類的例子,它們很可能已經過時了,但ArrayList已經取代了很多年了。 – berry120 2013-03-05 13:52:49

回答

1

我不會給你準確的答案,因爲這看起來非常像功課,但這裏是一個示例實施一家酒店使用arr AYS。通過它並嘗試理解它;如果你這樣做,你可以自己完成病人問題。

import java.util.*; 

class Customer 
{ 
    private String name; 
    private int room; 

    public void setName(String name) 
    { 
     this.name=name; 
    } 

    public String getName() 
    { 
     return this.name; 
    } 

    public void setRoom(int room) 
    { 
     this.room=room; 
    } 

    public int getRoom() 
    { 
     return this.room; 
    } 
} 

class Hotel 
{ 
    public static void initialize(Customer RoomList[]) 
    { 
     for(int i=0; i<RoomList.length; i++) 
     { 
      RoomList[i]=new Customer(); 
      RoomList[i].setName("EMPTY"); 
      RoomList[i].setRoom(i+1); 
     } 
    } 

    public static void viewList(Customer RoomList[]) 
    { 
     for(int i=0; i<RoomList.length; i++) 
     { 
      if(RoomList[i].getName()=="EMPTY") 
       System.out.println("Room number "+RoomList[i].getRoom()+" is vacant."); 
      else 
       System.out.println("Room number "+RoomList[i].getRoom()+" is ocupied by "+RoomList[i].getName()+"."); 
     } 
     System.out.println(); 
    } 

    public static boolean addCustomer(Customer RoomList[], String name) 
    { 
     for(int i=0; i<RoomList.length; i++) 
      if(RoomList[i].getName().equals("EMPTY")) 
      { 
       RoomList[i].setName(name); 
       return true; 
      } 
     return false; 
    } 

    public static void showEmptyRooms(Customer RoomList[]) 
    { 
     System.out.println("Available rooms are:"); 
     for(int i=0; i<RoomList.length; i++) 
      if(RoomList[i].getName()=="EMPTY") 
       System.out.println(RoomList[i].getRoom()); 
     System.out.println(); 
    } 

    public static boolean deleteCustomer(Customer RoomList[], String name) 
    { 
     for(int i=0; i<RoomList.length; i++) 
      if(RoomList[i].getName().equals(name)) 
      { 
       RoomList[i].setName("EMPTY"); 
       System.out.println("Deletion successful.\n"); 
       return true; 
      } 
     return false; 
    } 

    public static int getIndex(Customer RoomList[], String name) 
    { 
     for(int i=0; i<RoomList.length; i++) 
      if(RoomList[i].getName().equals(name)) 
       return i; 
     return -1; 
    } 

    public static void main(String[] args) 
    { 
     int numOfCustomers=10; 
     Customer[] RoomList = new Customer[numOfCustomers]; 
     String name; 
     initialize(RoomList); 
     Scanner input = new Scanner(System.in); 
     int option=0; 

     do 
     { 
      System.out.println("  Hotel Booking Options"); 
      System.out.println("====================================="); 
      System.out.println("1: To View all rooms"); 
      System.out.println("2: To Add customer to a room"); 
      System.out.println("3: To Display empty rooms"); 
      System.out.println("4: To Delete customer from a room"); 
      System.out.println("5: Find room from customer name"); 
      System.out.println("0: Exit"); 

      System.out.print("\nEnter your choice: "); 
      option = input.nextInt(); 
      System.out.println(); 

      switch(option) 
      { 
       case 1: 
       { 
        viewList(RoomList); 
        break; 
       } 
       case 2: 
       { 
        System.out.print("Customer's name: "); 
        name=input.next(); 
        System.out.println(); 
        if(!addCustomer(RoomList, name)) 
         System.out.println("No rooms available!"); 
        break; 
       } 
       case 3: 
       { 
        showEmptyRooms(RoomList); 
        break; 
       } 
       case 4: 
       { 
        System.out.print("Customer's name: "); 
        name=input.next(); 
        System.out.println(); 
        deleteCustomer(RoomList, name); 
        break; 
       } 
       case 5: 
       { 
        System.out.print("Customer's name: "); 
        name=input.next(); 
        System.out.println(); 
        System.out.println("Customer's room: "+RoomList[getIndex(RoomList, name)].getRoom()+"\n"); 
        break; 
       } 
       case 0: 
       { 
        System.out.println("\nThank you!\n"); 
        break; 
       } 
       default: 
       { 
        System.out.println("Invalid option!\n"); 
        break; 
       } 
      } 


     }while(option!=0); 
    } 
} 
+0

感謝Roney,它不是作業,它只是有時很難掌握一本書的內容。我認爲我更努力的是向數組中添加2個不同的字段,然後從其中一個字段返回值。我將通過您的示例來嘗試並將其中的一些應用於我正在嘗試的內容。非常感謝。 – user1295053 2013-03-05 14:39:02

+0

@ user1295053:我的錯誤。 :)無論如何,這個代碼應該清除這些疑惑,我想。 – 2013-03-05 14:42:48

5

這種信息有很好的記錄here

在你的情況,好像你要創建類型的數組Patient

用於創建可容納十名患者數組的語法是:

Patient[] patients = new Patients[10]; 

和之後閱讀文檔,你將能夠使用我提供的語法來獲得所需的功能:)

+0

謝謝你的幫助Chris – user1295053 2013-03-05 14:39:30

+0

是我的榮幸。 :) – christopher 2013-03-05 14:48:04