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

2012/11/28

What do we mean by regular array?

hkrhässleholm hkrhässleholm

2012/11/28

#
Hi friends! I am wondering about the defference between "regular array" and "list". By referring to my school work, my requirement is : "The snake shall be able to have an unlimited number of segments. This must be solved using a regular array, no Lists are allowed."
Gevater_Tod4711 Gevater_Tod4711

2012/11/28

#
Well an regular array has to be initialised with a clear number of fields and so it can't be unlimited. Maybe you can use an array with about 1000 fields so that the snake can't get this large but to have an unlimited snake you need to have a other data structur than an array. For Example you can use a queue: Thats a data structur where every part (of the snake in your case) knows the next part of it and so its unlimited. (until you have enough RAM)
hkrhässleholm hkrhässleholm

2012/11/28

#
Ok thanks, now can u look at my codes of "eat" method where a snake gets unlimited body segments by eating food. Can you explain, are the codes okay for regular array? private void eat() { Actor food=getOneObjectAtOffset(0,0,Food.class); if (food!=null){ SnakeEarth snakeEarth=(SnakeEarth)getWorld(); snakeEarth.removeObject(food); snakeEarth.addArray(); snakeEarth.snake = new Snake(); snakeEarth.addObject(snakeEarth.snake, getX(), getY()); length++; }
Gevater_Tod4711 Gevater_Tod4711

2012/11/28

#
You are using an array in this method but it can't be unlimited because the array snake in your world class has to be initialised with a maximum number of fields like this:
Snake[] snake = new Snake[theNumberOfMaximumFields];
And because of this maximum the array can't be unlimited. In this case it looks like it's unlimited if the number of fields is more than the fields of your world because then the snake can't grow anymore.
hkrhässleholm hkrhässleholm

2012/11/28

#
Ok, but My game works fine and the snake gets unlimited body segments...
Gevater_Tod4711 Gevater_Tod4711

2012/11/28

#
Probably your array is that big that you never reach the maximum.
hkrhässleholm hkrhässleholm

2012/11/28

#
Ok, then tell me, the array i used is a regular array or not?
Gevater_Tod4711 Gevater_Tod4711

2012/11/28

#
Yes it is a regular array. In your World class the array is declared like this:
Snake[] snake = new Snake[anyNumber];
and every time you use this brackets: you are using a regualr array.
Malmotri Malmotri

2012/11/28

#
You call your metod from snake class to world class with this line. snakeEarth.addArray(); This is the code probably that makes it happen so you can have unlimited body segments.
public void addArray()

    {
        Snake[] newSnake =  new Snake[snake.length  +1];
        System.arraycopy(snake, 0, newSnake, 0, snake.length);
        snake = newSnake; 

    }
hkrhässleholm hkrhässleholm

2012/11/28

#
Thanks, i think i got My expected answer
You need to login to post a reply.