• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create an array of MY class  RSS feed

 
Ryan Tracey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there Java Forums I don't know how to create an Array of my class Waypoints...

The format to build a Waypoint is (int,int)

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
You create an array of your objects the same way you create an array of anything else.
Option 1:
 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well if you are using standard arrays you would want to put an element into that array

Have a look here it explains it : http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing you put there is going to be right. The right-hand side of the expression produces a Waypoint. Just a Waypoint. It doesn't produce an array of Waypoints, so you can't assign it to an array of Waypoints. So that doesn't make sense.

I'm guessing that, since you have a loop, your goal is to assign that Waypoint object to some entry of a Waypoint array. One object per entry. So you would declare and create the array outside the loop, before you start. And inside the loop, assign the object to an entry of the array. Somewhat like this:

There are two unknowns here: the number of entries you want to have in the array, and the "starting offset" to make the entry numbers start at zero instead of at j, which is where the loop variable starts. Array entry numbers always start at zero. I leave it as an exercise for you to figure those out.>
 
Ryan Tracey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, didn't bother posting all code but you gave me the answer - sat here for hours coding and messing my eyes up missing simple things. Please feel free to help on my next questions :p...
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For once, I got there first
I earlier wrote:Option 1
Let's try option 2. You can declare and fill an array with an array initialiser. Note the option 1 created the array, which had 3 elements but it was empty, so you had to use the for loop to put the 3 elements into it. Note also that after saying [3], you are stuck with 3 elements.You can put as many elements as you can write or spell correctly into that initialiser. Here you have 4, so you can replace any of them, but you are stuck with 4.

You can replace the reference with a reference to a new array whenever you wish, but remember the new array will be empty and must be filled before you try to use its references. There are at least two ways to do itThat snippet gives you a size 2 array, already filled. Note you can only miss out "new Waypoint[]" if you have the declaration in the same statement (as in first code sample in this post).

Look at the Java™ Tutorials and find the arrays section. It will probably help you.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! But just one thing... in my post I said you should declare and create the array outside the loop. But in the code I posted, I didn't do that. So the code I posted was wrong. The array declaration should come before the for-statement. Oops.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Tracey wrote:Thanks guys, . . .
You're welcome And I see James Elsey gave you a Java™ Tutorials link before I did.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!