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

2017/10/16

Need help with Array in constructor

troubleturtle troubleturtle

2017/10/16

#
Hello! I am attempting to put an array in a constructor, but when I attempt to define the array in the constructor, it always throws an error. Here is the code: private void prepare() { Alligator alligator1 = new Alligator(Greenfoot.getRandomNumber(10),(Greenfoot.getRandomNumber(10)),(Greenfoot.getRandomNumber(10))); addObject(alligator1,(Greenfoot.getRandomNumber(600)),Greenfoot.getRandomNumber(400)); } I also have the code for the Actor in case the problem is there. public class Alligator extends Actor { private int alligatorspeed; private int alligatordirection; private int alligatornumerofturns; private int alligatormove; public Alligator(int speed, int direction, int numerofturns, int move) { alligatorspeed = speed; alligatordirection = direction; alligatornumerofturns = numerofturns; alligatormove = move; } } Thanks!
danpost danpost

2017/10/16

#
The line creating the Alligator (in the 'prepare' method) is passing three int values -- not two int values and one int array. You can create an int array as follows:
new int[] { value1, value2, ... }
I did use code tags to show this, however, bear in mind that given is not a complete line of code -- just an example of how to create an int array within a line of code.
You need to login to post a reply.