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

2015/6/2

Help Me Understand Arrays

plant plant

2015/6/2

#

The Code Works FYI

I do not get this code at all. Could someone help me understand it in and out please. Please keep in mind I understand what an int is. I know what parameters are. I know what Booleans are. I know what addObject means. I just need to know more about the Arrays. I never really understood it in class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import greenfoot.*;
 
public class Tiles extends World{
    private Tile[] tiles;
    private int TRACKER;
    public Tiles(){   
        this(4);
    }
 
    public Tiles(int SIZE){
        super((SIZE+2)*50, (SIZE+2)*50, 1);
        TRACKER = 0;
        tiles = new Tile[(SIZE+2)*(SIZE+2)];
        setUp(SIZE);
    }
 
    private void setUp(int SIZE){
        for (int t = 0; t < tiles.length; t++){
            if (t < SIZE + 2 || t > tiles.length - (SIZE + 2))
                tiles[t] = new Tile(true); //true means the tile is flipped up
            else {
                if (TRACKER == SIZE + 1 || TRACKER == 0)
                    tiles[t] = new Tile(true);
                else
                    tiles[t] = new Tile(false);
                TRACKER++;
                if (TRACKER == SIZE + 2)
                    TRACKER = 0;
            }
        }
        int x = 25, y = 25, TRACKER = 0;
        for (int m = 0; m < tiles.length; m++){
            if (TRACKER == SIZE + 2){
                x = 25;
                y += 50;
                TRACKER = 0;
            }
            addObject(tiles[m],x,y);
            x += 50;
            TRACKER = TRACKER + 1;
        }
    }
}
plant plant

2015/6/2

#
Here is the tile class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import greenfoot.*;
import java.awt.Color;
 
public class Tile extends Actor{
    public Tile(boolean isABorder){
        int SIZE = 50;
        if (isABorder){
            GreenfootImage img = new GreenfootImage (SIZE,SIZE);
            img.setColor(new Color (155,0,0));
            //
            img.fill();
            img.setColor(Color.BLACK);
            img.fillOval(SIZE/4, SIZE/4, SIZE/2, SIZE/2);
            //
            setImage(img);
        } else {
            GreenfootImage img = new GreenfootImage (SIZE,SIZE);
            img.setColor(new Color (51,255,255));
            //
            img.fill();
            img.setColor(Color.BLACK);
            img.drawRect(0, 0, SIZE-1, SIZE-1);
            //
            setImage(img);
        }
    }
}
plant plant

2015/6/2

#
Btw The Instructions for the project are at http://www.pembinatrails.ca/fortrichmondcollegiate/compsci/greenfoot/gridbased/default.htm
danpost danpost

2015/6/2

#
Before trying to explain what an array of Tile objects (see line 4 of the Tiles class) is, let us start with something a little more basic (since you know what int and boolean are). An array is used to essentially store in memory more than one value (usually) using the same field name. The values stored are indexed (starting at zero) so that any value (or element) within the array can be directly accessed. These elements can be of one of the primitive types (int, double, boolean, char, etc.) or of a reference type (String, Font, Color, Tile, etc). Once an array is created, its size is immutable; this, however, does not prevent one from assigning an array of similar type and different length to the array field. To illustrate:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int[] values = new int[5]; // int array of size 5
for (int n=0; n<values.length; n++)
{
    values[n] = n*n;
}
// values in array are now { 0, 1, 4, 9, 16 }
values = new int[10]; // new array of different length assigned to 'values' array field
for (int n=0; n<values.length; n++)
{
    values[n] = n;
}
// values in array are now { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
values = new int[] { 2, 2, 3, 5, 8, 13, 21 }; // new array with given values assigned to 'values'
// array now has a size of 7 and holds the first 7 numbers in the Fibonacci  set
System.out.println("The sixth number of the Fibonacci set is: "+values[5]);
Notice that since the first index is zero, the sixth element is accessed by the number one less than that. An array can hold references to Object objects as well. Line 4 above declares an array of undetermined size that will hold Tile object references. The array must be initialized before being used (see line 13). Then, Tile objects can be stored within the array at specific indexed locations (see lines 20, 23 and 25). Later, they can be accessed (see line 38, which adds the Tile objects accessed from the array into the world).
You need to login to post a reply.