• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Filling an Arraylist with objects

 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble filling an Arraylist with objects that i defined as a Tile that are constructed from the Tile class. The TileManager class is supposed to store a list of tile objects in the Arraylist. This i can't seem to figure out.



The tile class looks as such...




Any help would be great as im trying to teach myself this!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't said what the problem is.
 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, sorry i wasn't very specific. Am i filling the ArrayList with the tile objects correctly?
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your code even compile? ;)
Hint: TileManager#17

I do not understand the utility of the TileManager. All it does is act as a wrapper for an ArrayList. Why do you need the TileManager? The same functionality can be achieved with a simple ArrayList without a wrapper around it. What are you trying to do here? Is it for academic and learning purposes? Maybe you plan to add functionality to the TileManager later on?
 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't compile but i believe i found what i did wrong. Ill write back if any other problems arise.
The purpose of TileManager is to store the tiles and (later on) use it to change the contents of the Arraylist like shuffling and moving the order of the objects around.
 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you suggest to fix the TileManager#17 issue? I haven't learned much about using arraylists..
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P Derlyuk wrote:How would you suggest to fix the TileManager#17 issue? I haven't learned much about using arraylists..

It has nothing to do with ArrayList but scope
Consider your code

What does your compile error say about #17? What is the scope of myArray declared in your constructor? Can it be "seen" from #17?
To solve the problem you need to make sure myArray is visible for line 17
Recommended reading: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha..
Would this work?


 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you compiled it successfully and you already know it will work! Well done!
One small change. It is always a good idea to restrict the scope of the instance variables.
Your myArray is now scoped as public. If you make it private, you have absolute control over what kind of data goes in, out or deleted from myArray. Your TileManager can provide getter/setter methods so data can be manipulated in the arraylist
 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!.
Im going to be working on those other methods so Ill probably be back.
 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you happen to know the best way to move objects around in an Arraylist? For example, if I have an Arraylist with [ a, b, c, d, e ],
how would I move one object to the front and others shift back like... [ c, a, b, d, e ].
 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would this code do it?
("item" is the object and is in for-loop)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it?
 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind, got it.

 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The methods in java.util.*; was "remove" and "add" not "RemoveAt" and "Insert".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic