Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Adding things to an array in a different class  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following class that contains an array of items read in from a JSON


I also have another class called Player that contains the players items.


Now I need to read in the rooms class items into the player classes items from within a different method. and cant figure out how to do it Below is what ive tried
 
Alex Karbarz
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alex Karbarz wrote:I have the following class that contains an array of items read in from a JSON


I also have another class called Player that contains the players items.


Now I need to read in the rooms class items into the player classes items from within a different method. and cant figure out how to do it Below is what ive tried


Almost forgot I also have the item class.
 
Ranch Hand
Posts: 186
1
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use what is known as Encapsulation in your Object-Oriented Programming design. You need to set your instance variables to private and then use what is known as getters and setters to invoke your variables in another class. Please let me know if you need further clarification.
 
Alex Karbarz
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Naziru Gelajo wrote:You need to use what is known as Encapsulation in your Object-Oriented Programming design. You need to set your instance variables to private and then use what is known as getters and setters to invoke your variables in another class. Please let me know if you need further clarification.



Perfect thank you! However, I do have one more question. I want to only add the item a user specifies. For example view the code below.
 
Marshal
Posts: 5980
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have to use arrays?  I would use an ArrayList for items (or a HashSet it you want to avoid duplicates) then create a method in the Player class called getItems() that returns the List (or Set).  Then you can do this:
And it would be even better it all the collections were Lists or Sets.
 
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Do you have to use arrays? . . .

I would go further than Knute and ask you to explain what you are trying to do. Once you have explained that, it will probably be easier to work out what data structure to use.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!