2014-05-04 8 views
0

我已經閱讀了不少有關xlint,不安全操作的東西以及很多它說沿着線的東西:不安全的操作...使用-Xlint重新編譯:unchecked ...我似乎無法理解爲什麼操作是不安全的

你可能初始化ArrayList中,

Arraylist x = new Arraylist(); 

代替,這樣做,

List<String> x = new ArrayList<String>(); 

這是所有罰款和花花公子,但不用這個si來幫助我tuation。在我的項目中,我試圖做一個基於回合的遊戲,並且我有一個Commander類,有一個單位和建築物的數組列表。單位是獨立的對象,帶有子類。在構造子類時,Commander對象是一個參數,所以這就是我使用它的原因,我認爲這很好?這裏是哪裏出了問題是:

public void buyUnit(int u) 
{ 
    if (u < 1 || u > 23) 
    { 
     if (u == 1) 
      this.getUnits().add(new Grunt(this)); 
     if (u == 2) 
      this.getUnits().add(new Rifleman(this)); 
     if (u == 3) 
      this.getUnits().add(new Scout(this)); 
     if (u == 4) 
      this.getUnits().add(new Mortar(this)); 
     if (u == 5) 
      this.getUnits().add(new FlakTrooper(this)); 
     if (u == 6) 
      this.getUnits().add(new RPG(this)); 
     if (u == 7) 
      this.getUnits().add(new Sniper(this)); 
     if (u == 8) 
      this.getUnits().add(new Minigunner(this)); 
     if (u == 9) 
      this.getUnits().add(new Humvee(this)); 
     if (u == 10) 
      this.getUnits().add(new Tank(this)); 
     if (u == 11) 
      this.getUnits().add(new Artillery(this)); 
     if (u == 12) 
      this.getUnits().add(new MissileBattery(this)); 
     if (u == 13) 
      this.getUnits().add(new GattlingGun(this)); 
     if (u == 14) 
      this.getUnits().add(new IFV(this)); 
     if (u == 15) 
      this.getUnits().add(new TankBuster(this)); 
     if (u == 16) 
      this.getUnits().add(new Flamethrower(this)); 
     if (u == 17) 
      this.getUnits().add(new Fighter(this)); 
     if (u == 18) 
      this.getUnits().add(new Bomber(this)); 
     if (u == 19) 
      this.getUnits().add(new Gunship(this)); 
     if (u == 20) 
      this.getUnits().add(new Gunner(this)); 
     if (u == 21) 
      this.getUnits().add(new Jet(this)); 
     if (u == 22) 
      this.getUnits().add(new Chopper(this)); 
     if (u == 23) 
      this.getUnits().add(new Harrier(this)); 
     this.loseBalance(units.get(units.size() - 1).getCost()); 
    } 

我敢肯定有一個更好的方式來做到這一點,而不是使用一個整數參數,但如果有,同時保持,要解決這個問題的方式我喜歡。最後一行,使用loseBalance(int b)方法很好,因爲我在發表評論時遇到錯誤。所以有什麼問題?


好Marco13指出出來的東西有關getUnits()方法,該方法是:

public ArrayList getUnits() 
{ 
    return units; 
} 

剛剛返回單位的ArrayList。我還是不明白,爲什麼現在的作品,但我切換:

this.getUnits(). ... 

units. ... 

我想這是愚蠢的試圖返回的ArrayList擺在首位的時候,我能夠在類中訪問它,但這仍然不能解釋爲什麼當我嘗試返回ecact相同的數組時,爲什麼會出現錯誤,但只能使用方法而不使用引用本身。

+0

這樣做是創建一個基本類型(接口或抽象類)您的遊戲類型和使用Java的方式,作爲泛型列表類型。另一方面,泛型是java的編譯時特性,只要你知道你在做什麼並且留意你的列表正在被使用的代碼的不同部分,忽略錯誤是安全的。 – Nazgul

+0

getUnits()方法是什麼樣的?發生錯誤的地方在哪裏? – Marco13

+2

這種方法太可怕了。刪除它,假裝你從來沒有寫過這麼難看的東西......我不確定你想要做什麼,但是一個case switch會更合理。更好的是,有訪客的工廠模式。 –

回答

2

您已使用生成警告的原始類型ArrayList

切勿將參數化類型與原始類型混合使用。

public ArrayList getUnits() 
{ 
    return units; 
} 
0

不太你是問,但如果你有instriction這樣的:

if (u < 1 || u > 23) 
    { 
     if (u == 1) 
      this.getUnits().add(new Grunt(this)); 
     if (u == 2) 
      this.getUnits().add(new Rifleman(this)); 
    } 

然後內,如果將永遠是真實的,因爲U具有小於1或大於23,以去測試那個ifs。 應該

if (u >= 1 && u <= 23) 

這是第一件事情,第二,使用的

if(u==1){ 
    //code 
}else if(u==2) { 
    //code 
} 

代替

if(u==1){} 
if(u==2){} 

,因爲要檢查他們的每個人即使是比賽已經。 你的情況我建議使用

switch(u) 
{ 
    case 1: 
     //code 
     break; 
    case 2: 
     //code 
     break; 
    case 3: 
     //code 
     break; 
    . 
    . 
    . 
    default: 
} 

沒有這種if (u < 1 || u > 23)

相關問題