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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:33:19+00:00 2026-05-23T20:33:19+00:00

I am going through Generics Tutorial and was going through example to copy objects

  • 0

I am going through Generics Tutorial and was going through example to copy objects from array to collection.

Code

    static void fromArrayToCollection(Object[] a, Collection<?> c) {
    for (Object o : a) { 
        c.add(o); // Compile time error
    }
}

I am thinking that I can pass object as parameter to collection and it should work without any issues but tutorial says

By now, you will have learned to avoid the beginner’s mistake of trying to use Collection as the type of the collection parameter.

Why does it say that passing Object as parameter type to Collection is not correct approach?
Updated:

    static void fromArrayToCollection(Object[] a, Collection<Object> c) {
    for (Object o : a) { 
        c.add(o); // Compile time error
    }
}
  • 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-23T20:33:20+00:00Added an answer on May 23, 2026 at 8:33 pm

    The “beginner mistake” they’re referring to is saying Collection<Object> when what you were trying to say is “any Collection/Collection of Anything.” It would in the abstract be perfectly legal to declare the method as Collection<Object> it just doesn’t meet the design goal of a method that takes in anything.

    We want to be able to do this:

    public static void main(String[] args) {
        String[] stringArray = {"A", "B", "C"};
        List<String> stringList = new ArrayList<String>();
        fromArrayToCollection(stringArray, stringList);
        Integer[] intArray = {1, 2, 3};
        List<Integer> intList = new ArrayList<Integer>();
        fromArrayToCollection(intArray, intList);
    }
    
    public static <T> void fromArrayToCollection(T[] array, Collection<T> collection) {
        for (T item: array) {
            collection.add(item);
        }
    }
    

    That wouldn’t work if you made it Collection<Object>.

    You can’t declare the parameter type as Collection<Object> and have it work for multiple types like above because generic types aren’t covariant. It would be illegal to say, pass in List<String> to a method with an argument type of Collection<Object>. A Collection<String> is not a Collection<Object>.

    Consider the standard example:

    List<Integer> intList = new ArrayList<Integer>();
    List<Object> objList = intList; //doom impending!!!
    objList.add("NOTANUMBER");
    int i = intList.get(0).intValue(); //runtime exception!
    

    That’s why it’s illegal to declare it as Collection<Object> and take a collection of anything.

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

Sidebar

Related Questions

I'm going through this tutorial from droid draw: http://www.droiddraw.org/tutorial3.html I'm getting this error when
I was going through TypeErasure topic at http://download.oracle.com/javase/tutorial/java/generics/erasure.html which says that compiler removes all
While going through university and from following the development of SO, I've heard a
I was going through some code and came across a scenario where my combobox
I've been going through some old code, where I came across some custom defined
I am going through a book trying to understand Generics with C# and I
I'm going through someone else's code and came across the following syntax: typedef struct
I noticed this while using Qt, going through the code examples. When they try
After going through the Appendix A, C# Coding Style Conventions of the great book
I'm going through the problems on projecteuler.net to learn how to program in Erlang,

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.