Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Gjorgi Var
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 85
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 7023
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic