• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How to create an array of MY class

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Marshal
Posts: 76450
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 231
Android IntelliJ IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 27371
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76450
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 27371
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76450
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You have to be odd to be #1 - Seuss. An odd little ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic