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 6237583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:03:27+00:00 2026-05-24T11:03:27+00:00

How to declare mixedList with generics for such snapshot without modifying the rest of

  • 0

How to declare mixedList with generics for such snapshot without modifying the rest of the code?

List mixedList = new ArrayList();
if(flagA) {
    ClassA a = new ClassA(); //comes from elsewhere
    mixedList.add(a)
} else {
    List<ClassB> bList = new ArrayList<ClassB>();  //comes from elsewhere
    mixedList = bList; //error
}

I can do:

List<Object> mixedList = new ArrayList<Object>();
if(flagA) {
    ...
} else {
    ...
    mixedList.addAll(bList);
}

but is there a way to avoid changing the code?

  • 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-24T11:03:28+00:00Added an answer on May 24, 2026 at 11:03 am

    It’s not safe to assign bList (List<ClassB>) to mixedList (List<Object>).

    The service from which you obtained bList might retain a reference to it; this service will assume its list contains only ClassB instances. If you were allowed to assign that list to a List<Object> reference, you could then add any type of object to the list without a warning. But when the service, thinking that every element in its list was a ClassB, attempted to access the elements, a ClassCastException would be raised.

    Creating a new List<Object>, and adding elements to it with add() or addAll(), prevents this “type pollution”. You can safely modify this copy of the list, and let the source of the list keep its own copy “pure.”

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

Sidebar

Related Questions

Declare: LPWSTR** lines= new LPWSTR*[totalLines]; then i set using: lines[totalLines]=&totalText; SetWindowText(totalChat,(LPWSTR)lines[totalLines]); totalLines++; Now I
Why does Visual Studio declare new classes as private in C#? I almost always
I declare an NSArray in my code then building the array from another array.
I Declare Web Page Anonymous via entering this Code, <location path=Routing.aspx > The Above
I declare a new struct with the name of Struct I have a generic
Declare @BadDecimal varchar(5) Set @BadDecimal = '4.5' Declare @GoodDecimal Decimal Set @GoodDecimal = @BadDecimal
DECLARE @table table(XYZ VARCHAR(8) , id int) INSERT INTO @table SELECT '4000', 1 UNION
declare @servername varchar(2000) set @EmriServerit=(select @@servername) declare @dbname varchar(2000) set @dbname ='Test1' declare @Dir
declare @d varchar set @d = 'No filter' if (@d like 'No filter') BEGIN
I declare a variable for a 64 bit counter as : long long call_count;

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.