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

The Archive Base Latest Questions

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

I came across something very basic but extremely bewildering today. I needed to convert

  • 0

I came across something very basic but extremely bewildering today. I needed to convert a list to an array. The list contained String instances. Perfect example of using List.toArray(T[]), since I wanted a String[] instance. It would not work, however, without explicitly casting the result to String[].

As a test scenario, I used the following code:

import java.util.Arrays;
import java.util.List;

public class MainClass {
    public static void main(String args[]) {
        List l = Arrays.asList("a", "b", "c");
        String stuff[] = l.toArray(new String[0]);
        System.err.println(Arrays.asList(stuff));
    }
}

which does not compile. It’s nearly an exact copy of the example in the javadoc, yet the compiler says the following:

MainClass.java:7: incompatible types
found   : java.lang.Object[]
required: java.lang.String[]
    String stuff[] = l.toArray(new String[0]);
                          ^

If I add a cast to String[] it will compile AND run perfectly. But that is not what I expect when I looked at the signature of the toArray method:

<T> T[] toArray(T[] a)

This tells me that I shouldn’t have to cast. What is going on?

Edit:

Curiously, if I change the list declaration to:

List<?> l = Arrays.asList("a", "b", "c");

it also works. Or List<Object>. So it doesn’t have to be a List<String> as was suggested. I am beginning to think that using the raw List type also changes how generic methods inside that class work.

Second edit:

I think I get it now. What Tom Hawtin wrote in a comment below seems to be the best explanation. If you use a generic type in the raw way, all generics information from that instance will be erased by the compiler.

  • 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-12T00:33:16+00:00Added an answer on May 12, 2026 at 12:33 am

    you forgot to specify the type parameter for your List:

    List<String> l = Arrays.asList("a", "b", "c");
    

    in this case you can write safety:

    String[] a = l.toArray(new String[0]);
    

    without any cast.

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

Sidebar

Related Questions

Came across something like this today, and was wondering if there was an equivalent
While coding very simple program for removing blanks, tabs, newlines I came across something
I was refactoring some code in a web application today and came across something
I came across a frustrating something in Haskell today. Here's what happened: I wrote
I'm encountering something a bit bizarre, but maybe someone else came across this before.
I am looking at some code and came across something extremely unfamiliar to me
I came accross something very strange when i was testing my pages in FireFox.
Whilst trawling through some old code I came across something similar to the following:
I was reading some Java recently and came across something (an idiom?) new to
So while rewriting some code, I came across something along the lines of: Method

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.