2017-04-16 115 views
-3

我只是創建一個隨機的Java GUI應用程序,它接受客戶的詳細信息並希望將其存儲在一個數組列表中。我已經完成了我所知道的這個簡單的代碼,但它不起作用。誰能幫我這個。先謝謝你。這是我下面的代碼:從文本字段創建一個ArrayList

ArrayList<Database> PropertyList= new ArrayList<Database>(); 
    Database database= new Database(PropertyType,Address,Area,NoOfBedroom,NoOfToilets,Garage,OwnerName,OwnerAddress,OwnerPhoneNo,OwnerEmail); 
    PropertyList.add(database); 
    for (int i = 0 ; i<5 ;i++){ 
     System.out.println(PropertyList.get(i).getAddress()); 
    } 
+1

您在'PropertyList'中沒有添加5個數據庫類型的實例。你也應該在'Database'類中使用'getAddress()'。 –

+1

你想做什麼, 基本上你的arrayList只有一個對象,如果你迭代這個arrayList 你會得到** IndexOutOfBoundException ** –

回答

0

正如其他用戶所指出的,你只有一個對象在你的ArrayList和IndexOutOfBoundException時會發生I> = 1這個函數調用PropertyList.get(I)。

如果您不確定ArrayList中有多少元素,您應該使用for循環條件中的ArrayList .size()方法。

for (int i = 0 ; i < PropertyList.size(); i++){ 
    System.out.println(PropertyList.get(i).getAddress()); 
} 
+0

感謝傑恆爲我們提供的幫助。它爲我工作再次感謝 – user3515129