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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:51:07+00:00 2026-06-11T22:51:07+00:00

I intend to create a simple Dictionary ADT using a linked list. I have

  • 0

I intend to create a simple Dictionary ADT using a linked list. I have got little problem with the getKeys() method, here is my code:

@Override
public K[] getKeys() 
{
    if(head==null)
    return null;
    else
    {
        Vector<K> v = new Vector();
        ListNode<K,V> temp= head;
        while(temp!=null)
        {
            v.add(temp.key);
            temp=temp.next;
        }
        //K keys[] = new O[v.size()];

        return (K[])v.toArray();//run time error
    }
}

I am having the following error:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
    at Dictionary.ListDictionary.getKeys(ListDictionary.java:17)
    at Dictionary.DictionaryDriver.test(DictionaryDriver.java:83)
    at Dictionary.DictionaryDriver.main(DictionaryDriver.java:107)
Java Result: 1

Here is the interface i am trying to implement:

public interface DictionaryInterface <K extends Comparable<K>, V>
{
    public void insert(K key, V value);
    public V getValue(K str);
    public void remove(K key);
    public K[] getKeys();   
}

I understand that we cannot create an array of generics, but i never had problem with casting generic types to Objecttype.Does it have something to do with the generic type K extending Comparable? How do i work around this??

  • 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-11T22:51:08+00:00Added an answer on June 11, 2026 at 10:51 pm

    Try something like:

    return (K[])v.toArray(new Comparable[v.size()]);
    

    As a side note, however, mixing generics and arrays is not the best idea.


    To do this safely (and be able to accept types other than Comparable), you would need to get the type reference, so your constructor could look like this:

    public Dictionary(Class<K> keyType) {
        this.keyType = keyType;
    }
    

    Later on, when instantiating that array, call:

    return (K[]) v.toArray( Array.newInstance(keyType, v.size()) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple list view with adapter. I create 10+ listviewitems dynamically. Then
I intend to create asp.net pages using Visual Studio 2008. Preferably, the pages should
I have a data that looks like this . And I intend to create
The developer documentation has seemed to have failed me here. I can create a
I currently have a simple list view adapter that holds two rows of text.
I want to create a simple video player using mediaPlayer.. So I did the
I want to do a simple app using stdin. I want to create a
I am trying to create a simple list view with a search box at
I have been trying to create a simple application that will let the user
I am using an array list, a listView, and a custom simple adapter in

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.