How to assign arrays to variables?

Here I have 1 array with two different iterations of the possible shapes it can create. How can I assign these shapes to variables that I would then cycle through using the random function?

For example, the first shape would be 1, then second shape would be 2 etc. random(1,2,3etc)

  shape[0] = new Cell(4, 0, 35, true);
  shape[1] = new Cell(5, 0, 35, true);

  shape[0] = new Cell(5, 0, 35, true);
  shape[2] = new Cell(5, 1, 35, true);

Answers

  • final Cell[] cells = new Cell[4];
    Cell cell = cells[(int) random(cells.length)];
    
  • in line 4 : why not shape[2] and line 5 : shape[3] ?

    you don't need to assign arrays to variables

    if you like, each entry in the array (each line in the array) is a variable already

    hence you can treat an entry like shape[2] like a normal variable

    Random

    now your question is:

    using the random function?

    just say

    int currentShapeIndex = int(random(shape.length)); 
    

    an index is the line number / entry number of an array (it's the number in the [] part

    and currentShapeIndex after the line holds a random line number of the array. So use it:

    shape[currentShapeIndex].display(); 
    

    or whatever

Sign In or Register to comment.