anonymous-dev
anonymous-dev

Reputation: 3489

All my objects change in my arraylist

I'm adding a new object(class) to an ArrayList, but when I try to get variables from the objects in my ArrayList, the variables are the same in all my classes. I've added a NEW object, so I expect it to add a new object, right?

This is my code in my for loop. The print says that every variable in the object[i] has the same number.

ArrayList treeDots;
ArrayList branchList;
boolean clicked; 

void setup ()
{
    size(1024, 768, P3D);
    clicked = false;
    treeDots = new ArrayList();
    branchList = new ArrayList();
    treeDots.add(new TreeDot(width/2, height/2));
}

void draw ()
{
    if (clicked)
        AddTreeDot();

    if (treeDots.size() > 1)
    {
        for (int i = 0; i < treeDots.size() -1 ; i++)
        {
            int temp_loc = 0;
            TreeDot index1 = (TreeDot)treeDots.get(i);
            TreeDot index2 = (TreeDot)treeDots.get(i + 1);
            print(index1.xLoc(temp_loc)  + " ");
            print(i + " ");
            print(index2.xLoc(temp_loc)  + " ");
            print(i + " ");
            strokeWeight(2);
            stroke(#09FF00);
            line(index1.xLoc(temp_loc),index1.yLoc(temp_loc), index2.xLoc(temp_loc), index2.yLoc(temp_loc));
        }
    }
}

void AddTreeDot ()
{
    int randomX = 0;
    int randomY = 0;
    treeDots.add(new TreeDot(randomDotX(randomX), randomDotY(randomY)));
    clicked = false;
}

int randomDotX (int _randomX)
{
    TreeDot temp = (TreeDot) treeDots.get(treeDots.size() -1);
    int temp_x_loc = 0;
    int lastDotX = temp.xLoc(temp_x_loc);
    _randomX = lastDotX + int(random(-10, 10));
    return _randomX;
}

int randomDotY (int _randomY)
{
    TreeDot temp = (TreeDot) treeDots.get(treeDots.size() -1);
    int temp_y_loc = 0;
    int lastDotY = temp.yLoc(temp_y_loc);
    _randomY = lastDotY + int(random(0, 10));
    return _randomY;
}

void mouseClicked ()
{
    clicked = true;
}

and here is my class code

int randomSpread;
boolean canIGrow;
boolean endDot;
int x_loc;
int y_loc;
int lineThickness;

class TreeDot
{
    TreeDot (int x_loc_par, int y_loc_par)
    {
        x_loc = x_loc_par;
        y_loc = y_loc_par;
        //ellipse(x_loc_par, y_loc_par, 10, 10);
        endDot = false; 
    }

    int xLoc (int _x_loc)
    {
        _x_loc = x_loc;
        return _x_loc;
    }

    int yLoc(int _y_loc)
    {
        _y_loc = y_loc;
        return _y_loc;
    }
}

Upvotes: 0

Views: 81

Answers (3)

Wajeb
Wajeb

Reputation: 83

Class variables should be declared inside the class. What you're doing is you're setting the same (global) variables each time you add a new TreeDot.

P.S. your getters are not the problem as some of the answers are mentioning.

Upvotes: 1

steven35
steven35

Reputation: 4017

Your TreeDot getters/setters are messed up. In xLoc() you are passing in _x_loc, making it equal to x_loc and return it. Same for yLoc()

Upvotes: 1

StackFlowed
StackFlowed

Reputation: 6816

The problem that you have to declare variable inside the class & this code will be prblematic.

int xLoc(int _x_loc)
{
    _x_loc = x_loc;
    return _x_loc;
}

int yLoc(int _y_loc)
{
    _y_loc = y_loc;
    return _y_loc;
}

Then in call it

int temp_loc = 0;
TreeDot index1 = (TreeDot)treeDots.get(i);
TreeDot index2 = (TreeDot)treeDots.get(i + 1);
print(index1.xLoc(temp_loc)  + " ");
print(i + " ");
print(index2.xLoc(temp_loc)  + " ");
print(i + " ");

This line index1.xLoc(temp_loc) reset the value for you to 0.

Upvotes: 3

Related Questions