2015-11-11 169 views
1

我有一個10個對象的數組,我想初始化他們的數組。如何初始化數組的對象?

private Flight[] flight = new Flight[10]; 

flight[0] = new Flight("AA101", "Boston", "Sacremento", 421.33, 22, 6); 
flight[1] = new Flight("AA102", "Lincolin", Jacksonville, 612.08, 22 6); 
flight[2] = new Flight("AA103", "Rochester", "Columbia", 510.83, 22, 6); 
flight[3] = new Flight("AA104", "Boise", "Aspen", 411.12, 22, 6); 
flight[4] = new Flight("AA105", "Fort Collins", "Louiseville", 654.22, 22, 6); 
flight{5} = new Flight("AA106", "Seattle", "Dallas" 326.14, 22, 6); 
flight{6} = new Flight("AA107", "Phoenix", "Little Rock", 974.22, 22, 6); 
flight[7] = new Flight("AA108", "Cheyenne", "Louiseville", 521.43 , 22, 6); 
flight[8] = new Flight("AA109", "Louiseville", "Buffalo", 452.21, 22, 6); 
flight[9] = new Flight("AA100", "Anchorage" , "Tampa", 874.72, 22, 6); 

,但我得到噸的錯誤,如:

ReservationGUI.java:31: error: ']' expected

ReservationGUI.java:31: error: illegal start of type

ReservationGUI.java:31: error: ';' expected

ReservationGUI.java:31: error: expected

有他們的100。

+0

'Jacksonville'應該有引號,除非它引用變量的地方 – JonK

+1

移動所有這些這些線(除了第一個)的一些方法(或構造函數)內 – Eran

+0

葉蘭可能你詳細說明我們的方法,我不喜歡改變我的代碼,除非我完全明白我爲什麼要以某種方式做到這一點 –

回答

5

這裏有幾個不同的錯誤。

private Flight[] flight = new Flight[10]; 

private,我假設這不是一個方法。如果它在方法內,請刪除private。否則,一切都應該進入你的構造函數。您只能在方法外部使用變量聲明。

flight[1] = new Flight("AA102", "Lincolin", Jacksonville, 612.08, 22 6); 

您需要引用「傑克遜維爾」,因爲它應該是String。你還缺少一個逗號22

flight{5} = new Flight("AA106", "Seattle", "Dallas" 326.14, 22, 6); 

後,您使用的是在這條線(和其他幾個人)錯誤支架。它應該是flight[5],而不是flight{5}[]用於索引到數組中。 「達拉斯」之後你也錯過了一個逗號。

這裏有一個修正版本:

private Flight[] flight = new Flight[10]; 

public YourClassName(){ 
    flight[0] = new Flight("AA101", "Boston",  "Sacremento", 421.33, 22, 6); 
    flight[1] = new Flight("AA102", "Lincolin",  "Jacksonville", 612.08, 22, 6); 
    flight[2] = new Flight("AA103", "Rochester", "Columbia",  510.83, 22, 6); 
    flight[3] = new Flight("AA104", "Boise",  "Aspen",  411.12, 22, 6); 
    flight[4] = new Flight("AA105", "Fort Collins", "Louiseville", 654.22, 22, 6); 
    flight[5] = new Flight("AA106", "Seattle",  "Dallas",  326.14, 22, 6); 
    flight[6] = new Flight("AA107", "Phoenix",  "Little Rock", 974.22, 22, 6); 
    flight[7] = new Flight("AA108", "Cheyenne",  "Louiseville", 521.43, 22, 6); 
    flight[8] = new Flight("AA109", "Louiseville", "Buffalo",  452.21, 22, 6); 
    flight[9] = new Flight("AA100", "Anchorage", "Tampa",  874.72, 22, 6); 
} 

或者,你可以聲明數組中的一行,像這樣:

private Flight[] flight = {new Flight("AA101", "Boston", "Sacremento", 421.33, 22, 6), 
          new Flight("AA102", "Lincolin", "Jacksonville", 612.08, 22, 6), 
          /* The rest of your flights */ 
          new Flight("AA100", "Anchorage", "Tampa", 874.72, 22, 6)}; 

這會將所有Flight聲明,並填充陣列,成數組本身的聲明。這可以讓你初始化它而不用將它移動到一個方法中。欲瞭解更多信息,你可以read this。最終結果將是相同的,但它是一個稍微簡潔一點,並且不太容易出錯的方式來編寫它。如果您決定希望數組的內容是其他內容,它也使以後更容易更改。

0

使用方括號,而不是5大括號和6元

0

你有很多錯別字,和所有的編程語言是關於錯別字非常無情。

另一種方法:

private Flight[] flight = new Flight[] { 
    new Flight("AA101", "Boston", "Sacremento", 421.33, 22, 6), 
    new Flight("AA102", "Lincolin", "Jacksonville", 612.08, 22, 6), 
    new Flight("AA103", "Rochester", "Columbia", 510.83, 22, 6), 
    new Flight("AA104", "Boise", "Aspen", 411.12, 22, 6), 
    new Flight("AA105", "Fort Collins", "Louiseville", 654.22, 22, 6), 
    new Flight("AA106", "Seattle", "Dallas", 326.14, 22, 6), 
    new Flight("AA107", "Phoenix", "Little Rock", 974.22, 22, 6), 
    new Flight("AA108", "Cheyenne", "Louiseville", 521.43, 22, 6), 
    new Flight("AA109", "Louiseville", "Buffalo", 452.21, 22, 6), 
    new Flight("AA100", "Anchorage" , "Tampa", 874.72, 22, 6) 
};