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

  • Home
  • SEARCH
  • 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 8702419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:38:56+00:00 2026-06-13T02:38:56+00:00

Imagine having 2 Generic ArrayLists each storing different types. My program will be using

  • 0

Imagine having 2 Generic ArrayLists each storing different types. My program will be using only one of these ArrayLists at a time. Is it possible to create a general ArrayList (currentArrayList) which can store both ArrayLists, and use the two ArrayLists without casting.

ArrayList<Integer> arrInt = new ArrayList<>();
arrInt.add(10);
ArrayList<String> arrString = new ArrayList<>();
arrString.add("ten");

ArrayList<XXX> currentArrayList = arrInt;
Integer i = currentArrayList.get(0);

currentArrayList = arrString;
String str = currentArrayList.get(0);

Thanks.

  • 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-06-13T02:38:58+00:00Added an answer on June 13, 2026 at 2:38 am

    The problem is the part where you want to:

    use the two ArrayLists without casting.

    Definitely not going to happen. The whole point of a static type system is to stop you from being able to treat a general type as a specific type without explicitly asking to do so.

    So for example, you could say:

    ArrayList<?> currentArrayList = arrInt;
    Object i = currentArrayList.get(0);
    

    But notice that i is just an Object, so the compiler is happy – we don’t know anything about the things in the array except that they must be Object-based because this is a Java program. But you want to say:

    ArrayList<?> currentArrayList = arrInt;
    Integer i = currentArrayList.get(0);
    

    It’s that second line that isn’t ever going to happen without a cast.

    UPDATE Question from comments:

    Can’t the compiler easily infer the type of the ArrayList by
    looking at the generic type of arrInt ?

    Suppose it did that for us. What should it then do with the type of currentArrayList when it sees the line:

    currentArrayList = arrString;
    

    Your code as it stands assumes that the compiler is not going to do that kind of inference. Suppose you comment-out that second assignment. Now the compiler could make the inference you suggest, and allow your code to compile. But then in the future if you put back the second assignment, the rest of your code would stop compiling! This would be needlessly confusing.

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

Sidebar

Related Questions

I am having issues with what I can only imagine is a very simple
I'm having a tough time in finding a solution for following effect. Imagine having
I'm having some trouble with finalizing my MySQL query. Imagine that i combine the
I am having trouble trying to get iterators for inner sub-containers. Basically imagine this
imagine this html on a page <div id=hpl_content_wrap> <p class=foobar>this is one word and
I've recently completed an algorithmic sudoku solver in C, one in which three different
Quick question i imagine, but i'm having trouble getting a clear answer from googling.
Imagine having two list boxes on a form, where the choices in the second
Imagine having a main class -- Simulator -- that uses two other classes --
Imagine having 2 tables - Vehicles & Manufacturers. Vehicles (Id[PK], Manufacturer_Id[FK], Name, Price, Discount)

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.