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

2012/4/24

Arrays in Greenfoot

Klaus Klaus

2012/4/24

#
Hey can u help me pls. I have tried to make a array in greenfoot but i failed can u just send me one example that i can see how to make a array. nice greetings Klaus
SPower SPower

2012/4/24

#
Lets say you want to create an array of objects from the class AClass, this is how you do this:
AClass[] arrayName = {new AClass(), new AClass(), enz...}
Klaus Klaus

2012/4/24

#
.
Klaus Klaus

2012/4/24

#
can u pls explain it a little bit better??
SPower SPower

2012/4/24

#
You want to put some objects form the class AClass in the array. This is how you begin:
AClass[]
The name of the class needs to be here, and you have to put after it. Then, you write the name of the array:
arrayName
Then, you give it the objects, this just puts new AClass objects in it:
= {new AClass(), new AClass()];
SPower SPower

2012/4/24

#
You can only put the same type of objects in an array, I forgot to mention :) That's why you write this:
AClass[]
ttamasu ttamasu

2012/4/24

#
Arrays are simple collection of objects of the same type (where all decendents of an object can be considered the same type .. like all subclasses of Actor). Typically, you declare an array like so: Base_Type Array_Name = new Base_Type; where Base_Type is the class name like Actor, String,etc Array_name is any legal java name length is the size of the array you what to create eg. String names =new String; // creates an empty array with 10 slots starting at 0 and going to 9 Or you can initialize the values at the time of declaration likes so: String names = {"John","Mary","Bill"}; an arrray also has a predefined field call length which is the length of the current array. For example in a for statement you can write: for (int i = 0; i < names.length; i++) System.out.println (names); or in a for-each statement you can write for (String name: names) System.out.println(name);
You need to login to post a reply.