It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Filling an Arraylist with objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Filling an Arraylist with objects" Watch "Filling an Arraylist with objects" New topic
Author

Filling an Arraylist with objects

P Derlyuk
Ranch Hand

Joined: Feb 17, 2013
Posts: 33
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

Joined: Jan 10, 2002
Posts: 60765
    
  65

You haven't said what the problem is.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
P Derlyuk
Ranch Hand

Joined: Feb 17, 2013
Posts: 33
Oh, sorry i wasn't very specific. Am i filling the ArrayList with the tile objects correctly?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

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?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
P Derlyuk
Ranch Hand

Joined: Feb 17, 2013
Posts: 33
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

Joined: Feb 17, 2013
Posts: 33
How would you suggest to fix the TileManager#17 issue? I haven't learned much about using arraylists..
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

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

Joined: Feb 17, 2013
Posts: 33
Aha..
Would this work?


Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

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

Joined: Feb 17, 2013
Posts: 33
Thanks!.
Im going to be working on those other methods so Ill probably be back.
P Derlyuk
Ranch Hand

Joined: Feb 17, 2013
Posts: 33
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

Joined: Feb 17, 2013
Posts: 33
Would this code do it?
("item" is the object and is in for-loop)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

What happened when you tried it?
P Derlyuk
Ranch Hand

Joined: Feb 17, 2013
Posts: 33
Nevermind, got it.

P Derlyuk
Ranch Hand

Joined: Feb 17, 2013
Posts: 33
The methods in java.util.*; was "remove" and "add" not "RemoveAt" and "Insert".
 
 
subject: Filling an Arraylist with objects
 
Similar Threads
Graphics2D g2 = (Graphics2D)g;
JTextField/JPanel with round edged corners
Border on an rounded JTextField?
ColorTable Array!!
help with a class.