Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Array of objects  RSS feed

 
Tempora Telora
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a brain fart.

Can someone type up a small example of an array of objects (please in 2 different classes)
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a better idea, why don't you post what you have done so far and ask a specific question about what you are stuck on.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
 
Tempora Telora
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


is that the correct way to create an array of objects for a class?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.


should be

So, what you're doing, is creating a new array of size size and assigning a reference to the myArray variable, which is declared to hold references to Project_Table[] objects. (Note that "Project_Table[]" is the type)

Also, note that creating an array does not create the objects in the array. So your line

will not work for a variety of reasons. First, there is no Project_ID method on result set. You probably want to use getString("Project_ID") or getString(1). Second, there is no object into which to put the value, since the element at that point in the array is null.

Also, since you don't know how many objects you are going to have, an array is probably not the best choice here in any case. Try a List:

Then, if you need it as an array, it is possible to convert the list to an array.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!