• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 182
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.
 
Sheriff
Posts: 5121
138
Chrome Eclipse IDE Java Postgres Database 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: 60136
188
  • 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!