2012-05-09 55 views
0

我很难过,需要另一双眼睛来看看这个。此代码正在工作,并突然停止工作。基本上我将一个对象添加到数组列表中。当我构建列表时,我会观察它,并且似乎每次迭代都会添加一个独特的对象。基本上会出现在屏幕上的精灵和它的x,y坐标,颜色和速度。早些时候这个工作,并且精灵将出现在屏幕上,现在它似乎复制了添加到列表中的最后一个对象,X运行循环的次数我最终得到了相同的对象。这没有任何意义...引用一个java列表总是返回最后一个元素

第一个println语句打印什么传递给构造函数。所以它打印出来如此。

球:1×123 Y:344颜色:蓝色 球:2×:3 Y 233颜色:绿色 球3×:24岁:3颜色:蓝

一切看起来大为止。然后,我居然打印列表来安慰我得到

球:1 X:24 Y:3颜色:蓝色 球:1 X:24 Y:3颜色:蓝色 球:1 X:24 Y:3颜色:蓝色

哪就在于此,我想弄清楚这是为什么发生的问题...

//When I create the List Eclipse refused to accept it until I initialized it like so... 

    java.util.List <Sprite> sprite = new java.util.ArrayList<Sprite>();  
    //yes I did import java.util.*; Eclipse still was digging it. This was working correctly despite the way i added it. I also changed this to a Vector which Eclispe was more content with with no effect. 

    private void GenerateSprites(){ 
     //Random to keep it random 
     Random r = new Random(System.currentTimeMillis()); 
     //variables for selecting and setting color 
     Color color = null; 
    int colorValue; 
    //variables for their x,y coordinates 
    float bX = 0; 
    float bY = 0; 
    //Create each ball set the color and generate the x,y coordinates 
    for (int x = 0; x < NUM_BALLS; x++){ 
     colorValue = r.nextInt(4); 
     if (colorValue == 0) color = Color.BLUE; 
     if (colorValue == 1) color = Color.RED; 
     if (colorValue == 2) color = Color.YELLOW; 
     if (colorValue == 3) color = Color.GREEN; 

     bX = r.nextInt((int)(gameField.getWidth() - gameField.getWidth()/4)+SCRN_MARGIN); 
     bY = r.nextInt((int)(gameField.getHeight() - gameField.getHeight()/4)+SCRN_MARGIN); 

     //place the new ball in the gameField 
    //print the values being passed to the sprite constrcutor for debug purposes. The out put of this line indicates that all is well at this point.    
System.out.println("Ball: " + x + " X: " + bX+ " Y: " + (bY+SCRN_MARGIN) + " Color: " + color.toString()); 
     gSprite.add(new Sprite((float)bX, (float)bY+SCRN_MARGIN, BALL_SIZE, color)); 

    } 
    //Now that the sprites are added to this list print out the list. When this line executes it shows a list of NUM_BALLS all of which have the exact sdame vlaues as the last sprite added earlier. 
    for (int x = 0; x < gSprite.size(); x++){ 
     Sprite spr = gSprite.get(x); 

    System.out.println("Ball: " + x + " X: " + spr.getX()+ " Y: " + spr.getY() + " vX: " + spr.getvX() + " vY: " + spr.getvY() + " Color: " + spr.getColor().toString()); 
    } 

} 
+4

“此代码正在工作,并突然停止工作”...你有什么改变?代码永远不会停止*神奇地工作。 – talnicolas

+0

如果有异常,请添加堆栈跟踪... – Crazenezz

+0

每当您运行此代码时,什么打印到控制台? –

回答

0
bX = r.nextInt((int)(gameField.getWidth() - gameField.getWidth()/4)+SCRN_MARGIN); 

你要转让的整数浮动。请检查这一点,可能是因为数字四舍五入造成的问题

+0

我走过去并检查了这段代码,现在铸件都是正确的。仍然没有' – ACantrell

0

您需要检查hashcodeequals实施您的Sprite类。他们需要考虑Sprite的相关字段,因此两个不同的哈希码不会返回相同的哈希码,或者返回true来表示等于。我认为它适用于你使用默认实现(例如不覆盖它),但实现一个确定。在日食中,您可以选择SourceGenerate hashCode() and equals()。这应该不重要,但如果你真的使用ArrayList(我没有看到你的代码)。

我同意@Sanket,它可能是一个浮点数转换为整数的问题。也许它看起来像你每次都得到相同的一个?

此外,您应该使用Java 5,第二个循环的enhanced for-loop可以改写这样的(如预期那么甚至可能工作... ...):

int x = 0; 
for (Sprite spr : gSprite){ 
    System.out.println("Ball: " + x + " X: " + spr.getX()+ " Y: " + spr.getY() + " vX: " + spr.getvX() + " vY: " + spr.getvY() + " Color: " + spr.getColor().toString()); 
    x++; 
} 

而且,最后但并非最不重要的,你应该真的使用switch/case而不是四个ifs。这实际上不会帮助你的问题,但它只是不好的风格。请看:

switch (colorValue) { 
     case 0: 
      color = Color.BLUE; 
      break; 
     case 1: 
      color = Color.RED; 
      break; 
     case 2: 
      color = Color.YELLOW; 
      break; 
     case 3: 
      color = Color.GREEN; 
      break; 
     default: 
      throw new RuntimeException("Unexpected color!"); 
} 

BTW:还有其他的方法,如使用Enum或这种模式的Map。我只是认为使用switch/case有一个逻辑比特的优点超过2个ifs。当然,默认情况下处理不好,需要改进。

哦,还有一件事:你应该真的写方法名小写/骆驼大小写。基本上每个Java程序员都是这样的。

+0

感谢您的建议我通常遵循这些标准这是相当粗糙的代码,我一直在转换到更多的最终决定。问题基本上是这个方法添加到列表精灵的项目。它将五个精灵添加到这个列表中,并且它们似乎都是唯一的,但是当我稍后访问列表时,它们都将最后一个精灵的值添加到列表中......我写这篇文章是因为它在之前和之后工作知道我在路上做了一些事情,但我无法弄清楚我做了什么。在我开始搞乱之前,我可能应该复制一份。 – ACantrell

相关问题