Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

toArray() of ArrayList<String>

843793Jul 18 2004 — edited Jul 18 2004
   ArrayList<String> relNames;

   public String[] getRelNames()
   {
      if (database == null)
         throw new Error("No database is connected.");
     else
        return relNames.toArray();
   }
the code above wont compile because relNames.toArray(); returns Object[], but i ve specified in the declaration that ArrayList<String> relNames; dont quite understand why?

Comments

843793
the code above wont compile because
relNames.toArray(); returns Object[], but i ve
specified in the declaration that ArrayList<String>
relNames; dont quite understand why?
Object[] toArray() doesn't utilize generics. It always returns an array of Objects.

This should work.
ArrayList<String> relNames;

public String[] getRelNames()
{
   if (database == null)
      throw new Error("No database is connected.");
   else
      return relNames.toArray(new String[] {});  //use <T> T[] toArray(T[])
}
843793
Of course, if you had
ArrayList<T> relNames;
You will have to find a different solution. You can't create generic arrays.
1 - 2
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Aug 15 2004
Added on Jul 18 2004
2 comments
113 views