簡體   English   中英

引用Java列表始終返回最后一個元素

[英]Referencing a java list always returns the last element

我很困惑,需要另一雙眼睛看一下。 該代碼正常工作,突然停止工作。 基本上我是將對象添加到arraylist。 在構建列表時,我會看到它,並且似乎在每次迭代中都添加了一個唯一的對象。 基本上是一個精靈,它將顯示在屏幕上及其x,y坐標,顏色和速度。 之前的工作原理是,精靈會出現在屏幕上,現在看起來好像復制了添加到列表中的最后一個對象,X運行循環的次數我最終得到了同一對象。 這沒有任何意義...

第一個println語句打印將傳遞給構造函數的內容。 因此它按原樣打印。

球:1 x:123 y:344顏色:藍色球:2 x:3 y 233顏色:綠色球3 x:24 y:3顏色:藍色

到目前為止,一切看起來都很不錯。 然后我實際上將列表打印到控制台,然后得到

球:1 x:24年:3顏色:藍色球:1 x:24年:3顏色:藍色球:1 x:24年: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());
    }

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

您正在嘗試分配一個浮點數。 請檢查此內容,可能是由於數字舍入而造成問題

您需要檢查hashcodeequals Sprite類的實現。 他們需要考慮Sprite的相關字段,因此兩個不同的字段不會返回相同的哈希碼或等於true的true。 我認為,如果您使用的是默認實現(例如,不覆蓋它),它可以工作,但是請務必執行一個。 在eclipse中,您可以選擇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!");
}

順便說一句:還有其他方法,例如對這種模式使用EnumMap 我只是認為使用switch/case對於一個邏輯位具有2個以上ifs。 當然,默認情況處理不當,需要完善。

哦,還有另一件事:您應該真正編寫方法名稱為小寫/駝峰式。 基本上,每個Java程序員都采用這種方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM