This site requires JavaScript, please enable it in your browser!
Greenfoot back
Plop
Plop wrote ...

2018/10/30

What do the brackets do when surrounding the i in the code?

Plop Plop

2018/10/30

#
public synchronized static void initializeImages() 
    {
        if(images == null) 
        {
            GreenfootImage baseImage = new GreenfootImage("explosion-big.png");
            images = new GreenfootImage[IMAGE_COUNT];
            for (int i = 0; i < IMAGE_COUNT; i++)
            {
                int size = (i+1) * ( baseImage.getWidth() / IMAGE_COUNT );
                images[i] = new GreenfootImage(baseImage);
                images[i].scale(size, size);
            }
        }
    }
danpost danpost

2018/10/30

#
Plop wrote...
What do the brackets do when surrounding the i in the code?
The images field is declared to hold an array of GreenfootImage objects. Line 6 creates an array of length IMAGE_COUNT and assigns it to the field. The for loop says to count up to that number of images (the count, held in 'i' will be used to point to a particular image in the array, as an index). When you see square brackets like that in any java code, then an array is being dealt with.
You need to login to post a reply.