Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6333889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:33:12+00:00 2026-05-24T18:33:12+00:00

So, Iv just recently switched over from c++ to java for android programing, and

  • 0

So, Iv just recently switched over from c++ to java for android programing, and this may be a simple fix but I haven’t been able to figure it out yet 🙁 (and yes iv tried searching, although not 100% sure on the correct lingo, so may have missed it)

Pretty much, Iv got a

public static LinkedList<Object> LList = new LinkedList<Object>();

Defined that is used for rendering and animating objects. The problem is because I have just 1 linked list, and multiple object types I need to use if(LList.get(i) instanceof ship) for each object type.

For example

int i;   
    if(LList.size()>=1)
    {
        for(i=0;i<LList.size();i++)
        {
            if(LList.get(i) instanceof ship)
                ((ship) LList.get(i)).animate(elapsedTime);
            else if(LList.get(i) instanceof Laser)
                ((Laser) LList.get(i)).animate(elapsedTime);
        } 
    }

Call me crazy, but this does not seem like it is the best way. It works…but in c++ all you would simply have to do is make both ship and laser have the same parent then you could call .animate without worrying about the (ship/laser) typecast.

Is there a way to do this in java? I tried the same way as you would in c++ but that didn’t seem to work. keep in mind im new to java, so may have simply done it wrong.

I was although thinking that I could modify the LList.add function so you also pre-define the objects type as you add it, that would get rid of the instanceof problem, but not really add any flexibility.

Anyway, Thanks for your time!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:33:13+00:00Added an answer on May 24, 2026 at 6:33 pm

    There are a couple of ways to do this. The easiest is to define either a base class or an interface that defines the common functionality of all objects in your list. For instance, you might have:

    public interface Animatable {
        public void animate(long elapsedTime);
    }
    
    public class Ship implements Animatable {
        public void animate(long elapsedTime) {
            // implementation for Ship
        }
    }
    
    // etc for other classes
    

    Then you could write:

    public static LinkedList<Animatable> LList = new LinkedList<Animatable>();
    
    // ...
    
    for (Animatable object : LList) {
        object.animate(elapsedTime);
    }
    

    The same basic pattern would apply if you wanted to use a common base class, with the difference that if there was behavior that had a common implementation in all classes, it could be factored out into the base class.

    Another approach is to use the Visitor pattern. Although that is a bit harder to understand at first, it provides for much more flexibility and extensibility over the approach outlined above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We've just recently switched over from SVN to Mercurial, but now we are running
I've been using .NET Reflector for awhile but I've just recently switched machines and
We just recently switched from Visual Studio 2008 to Visual Studio 2010. I have
I recently switched over to C# from vb.NET and within visual studio found that
Recently I switched my templating engine from default to Jinja2/Coffin. Everything works just fine
I've recently switched over to handling my unhandled exceptions this way: Application.ThreadException += new
Just recently I switched the language of my project to use C++ from C.
I recently switched over from SVN to Hg. I used to use TortoiseSVN, and
Our project switched over from SVN to Mercurial recently. In SVN, to version our
[I'm sorry that this isn't directly a programming question. But I have recently switched

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.