Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Can we have something like myintarray[a] = 0 where "a" is a string value??

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can we have something like myintarray[a] = 0 where "a" is a string value??

    Say if we have String s = "abcdefg"

    is it possible to do something like this

    myArray[a] = 0
    myArray[b] = 1
    myArray[c] = 2
    myArray[d] = 3
    myArray[e] = 4
    .
    .
    .

    where a,b,c,d,e ... are string values

    Many Thanks


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Can we have something like myintarray[a] = 0 where "a" is a string value??

    No it isn't possible, as the values are integer pointers to data.
    You could however do myIntArray[Integer.valueOf("23")] = 2; if you so desired.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Can we have something like myintarray[a] = 0 where "a" is a string value??

    Quote Originally Posted by NickNickk View Post
    Say if we have String s = "abcdefg"

    is it possible to do something like this

    myArray[a] = 0
    myArray[b] = 1
    myArray[c] = 2
    myArray[d] = 3
    myArray[e] = 4
    .
    .
    .

    where a,b,c,d,e ... are string values

    Many Thanks
    What does the String s have to do with the rest of your question? Do you want the array indices to equal the index of the character? If so, String has methods to do just that. The API is your friend.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can we have something like myintarray[a] = 0 where "a" is a string value??

    Sounds like you want to use a Map. The String would be the key to get a value from the Map.
    value = theMap.get("a String");

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  2. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  3. Need something like a "Hashtable<String[], Integer>" kind of data type @@
    By Albretch Mueller in forum Java Theory & Questions
    Replies: 2
    Last Post: November 27th, 2010, 10:24 PM
  4. Replies: 1
    Last Post: January 15th, 2010, 01:32 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM