This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Can a string array member serve as an index number of another string array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can a string array member serve as an index number of another string array?" Watch "Can a string array member serve as an index number of another string array?" New topic
Author

Can a string array member serve as an index number of another string array?

Gjorgi Var
Ranch Hand

Joined: Feb 24, 2004
Posts: 85
Hola,
I really wonder if a value of a member of a string array could serve as an index number for some other string array...
Yes, an index of any array can only be int, but if an array of strings holds only numbers isn't it possible to go around this rule and have those numbers serve as indices?
Example:

Would like numbers[] members to be indices for letters[] members, without parseInt...
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
Hmm....you don't have a Perl background, do you?
The short answer...nope! You'll have to use parseInt or similar. Java is strongly typed, so where an 'int' is required, you gotta use an int (except as mentioned below).
There are two circumstances under which some form of conversion is done automatically for you:
  • Some automatic casting is done between numeric types (specifically, between primitive types byte, short, int, char, float and double)
  • When using inheritance (I won't explain here, the explanation is not short, and is rather off-topic.


  • All other conversion you gotta do yourself...
    Cheers,
    --Tim
    [ April 20, 2004: Message edited by: Tim West ]
    [ April 20, 2004: Message edited by: Tim West ]
    Gjorgi Var
    Ranch Hand

    Joined: Feb 24, 2004
    Posts: 85
    Casting a string to a byte array helped (getBytes() method), but code is far from elegant and from object oriented programing...
    Beside the casting that was previously mentioned, it also involves setting up an array for translating byte representation into the number that we started with (this time the number is int and not a string, and int is what I wanted)... if it is a double digit number, additional calculations apply...

    [ April 22, 2004: Message edited by: Gjorgi Var ]
    Dirk Schreckmann
    Sheriff

    Joined: Dec 10, 2001
    Posts: 7023
    Note that the Collections API privides various Map type data structures that allow for using one object as the key to a value object.
    If you're looking to learn more about such data structures, I suggest taking a look at the Collections Trail of Sun's Java Tutorial, as well as chapters 11 and 12 of David J. Eck's Introduction to Programming Using Java.


    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Can a string array member serve as an index number of another string array?
     
    Similar Threads
    Help with english to morse code program, I have no idea what I'm doing
    Cezar cipher problem
    help with creating random strings in an array
    how to generate random unique alphanumeric numbers
    Distance between words.