Dealing with ArrayLists in java...all members of the arraylist updating themselves?
        Posted  
        
            by Charlie
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Charlie
        
        
        
        Published on 2010-03-27T00:54:54Z
        Indexed on 
            2010/03/27
            1:03 UTC
        
        
        Read the original article
        Hit count: 401
        
I have a function that shrinks the size of a "Food bank" (represented by a rectangle in my GUI) once some of the food has been taken. I have the following function check for this:
public boolean tryToPickUpFood(Ant a)
    {
        int xCoord = a.getLocation().x;
        int yCoord = a.getLocation().y;
        for (int i = 0; i < foodPiles.size(); i++)
        {
            if (foodPiles.get(i).containsPoint(xCoord, yCoord))
            {
                foodPiles.get(i).decreaseFood();
                return true;
            }
        }
        return false;
    }
Where decreaseFood shrinks the rectangle..
public void decreaseFood()
    {
        foodAmount -= 1;
        shrinkPile();
    }
    private void shrinkPile()
    {
        WIDTH -=1;
        HEIGHT = WIDTH;
    }
However, whenever one rectangle shrinks, ALL of the rectangles shrink. Why would this be?
© Stack Overflow or respective owner