2014-05-04 43 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)

相关问题