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

How can I render all objects in an arraylist?  RSS feed

 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have several different objects that I want to render in an arraylist. They all have different arguments. For example, some render like this:

map.render(g);

and some render like this:

player.render(100,203);

If I place them all in an arraylist, how can I call each of their methods? If I make the arraylist hold Objects and use a for loop, it says something along the lines of "Object does not have a render() method".

If you need more information, please tell me what.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let say you do use Object, then at some point you need to check its type and cast to the proper class and call the correct render method. This approach surely not good, hard to develop and maintain.

Depending on exactly what your classes are, can you make an abstract class or interface with the render method. Then call this render method as you loop through the list.

Thinking about it abstract class seems to be better or easier I think to implement when you do looping.

If you use interface, then you probably need a factory or some class to actually instantiate the proper class and use this factory class for your looping.

Hope this make sense.
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes when you are calling the object in the ArrayList<Object> it either needs an explicit cast back to what it was to use that specific types methods. easiest here would be to use the instance of operator in an if else testing, esle as said above a good idea would be to make an abstract class with the signatures and make them all extend it.
 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each of the objects is different, and have different render() methods, so an abstract class or cast won't work.
 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind. I figured out that if I made an enhanced for loop for each type of object, I could go through all of the objects without it breaking on NullPointerExceptions.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alix Ollivier wrote:Each of the objects is different, and have different render() methods, so an abstract class or cast won't work.


That's exactly the case in which you should use a abstract class or interface. If every object implements or extends MyRenderable, which has a public abstract void render() method, then you can have List<Renderable>, and there's no need for any casting or any knowledge about the classes of the individual objects. This is exactly the reason for abstraction and polymorphism.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alix Ollivier wrote:Nevermind. I figured out that if I made an enhanced for loop for each type of object, I could go through all of the objects without it breaking on NullPointerExceptions.


While it's good to use an enhanced for loop, I don't know what you mean by doing it "for each type of object", but that doesn't sound right. Also, using an enhanced for loop has nothing to do with avoiding NPE. Whatever approach you use to iterate over the list, NPE should be no more or less an issue with that approach than with enhanced for.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alix Ollivier wrote:Each of the objects is different, and have different render() methods, so an abstract class or cast won't work.


When you first posted the problem, the suggestion was to use an abstract class or interface. So don't just say it won't work without first understanding the suggestion and trying it out.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!