JDBM put() then get() needs type casting?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    JDBM put() then get() needs type casting?

    Hey fellas, long time PHP programmer (yes you can roll your eyes) trying to do Java. Although I initially came from C++ and Java programming to PHP, I'm having trouble grasping a concept:

    I'm creating a jdbm hash table and putting in a String[] as the value, but when I try to get() it, it returns type Object and does not like being casted back to String[]. What is going on and why not?

    Code:
    hashtable = HTree.createInstance( recman );
    .
    .
    .
    String[] test= {"test hard, testhard2", "romeo must test", "live rich or test trying"};
    String[] romeo = {"romeo must die","romeo and juliet","romeo and juliet 2"};
    hashtable.put( "die", die );
    hashtable.put( "die", romeo );
    .
    .
    .
    // Error Line: 
    String[] titleArr = hashtable.get("die");
    
    Error: Uncompilable source code - incompatible types
    I've tried putting (String[]) explicit cast in front of the get, but still errors as saying:

    java.lang.Strin g cannot be cast to [Ljava.lang.Stri ng

    ---------------------------------------------


    FIXED before I finished my post, for anyone else who has the same issue. Experts, comments/feedback are welcome.

    Explanation of my bug:
    I was using an existing jdbm file that already had these keys with String values, not an array. recreated it and then the explicit casting worked. But I didn't use String[] anymore I used java.util.HashS et because my lists could get pretty large.

    Thanks,




    Dan
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You have to learn how to read those internal class names; the following list was bluntly stolen from the description of the Class.getName() method:

    boolean Z
    byte B
    char C
    class or interface Lclassname;
    double D
    float F
    int I
    long J
    short S

    If you read that error message again you'd see what had happened.

    kind regards,

    Jos

    Comment

    Working...