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 3 of 3

Thread: Using class implicit toString() for array index

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Using class implicit toString() for array index

    Hey all =)

    The title might not be what I really mean and for that I apologize..
    I'm asking this because its something that came up in a conversation I had with a colleague in my programming class

    Lets say I have this Class:

    public class Planet{
         int planetID;
     
         public Planet(int pID){
              planetID = pID;
         }
         public String toString(){
              return new Integer(planetID).toString();
         }
    }

    and this one:
    public class Testing{
         public static void main(String args[]){
              Planet mercury = new Planet(0);
              System.out.println(mercury);  //I know that this will return "0"
              Planet[] pArray = new Planet[9];
              pArray[mercury] = mercury;  //This is where my question lies
    }

    Saying in words, is it possible to use the implicit toString() every class has to directly return an int to make it usable in array indexing?

    I've already tried brainstorming with my colleague, and what we got was using something like(using above example):
    pArray[new Integer(mercury.toString())] = mercury;

    Is there an easier way to do this or is this simply stupid?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Using class implicit toString() for array index

    This is somewhat a similar concept to a Hash, in which a particular property of an object (in java the returned value of hashCode()) is used to lookup that object from some sort of group of objects. In your case why mess with the string/integer converion intermediates - you could just write a get method for the planetID and use that get in the array
    pArray[mercury.getID()] = mercury;

  3. The Following User Says Thank You to copeg For This Useful Post:

    Quetzalma (February 3rd, 2010)

  4. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using class implicit toString() for array index

    I'll have a look about the Hash on the Java API..

    yeah, we thought of that too, we just wanted to know if there was a way to use directly (in this case) 'mercury' instead of calling methods to return what we needed to index the array, but I suppose that would be asking too much of java for a simple .getID() :p

    Thanks for the reply

Similar Threads

  1. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 06:44 AM
  2. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 12:19 PM
  3. Method Adding elements to an array with certain restrictions
    By Newoor in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2009, 11:13 AM
  4. Converting a method from ArrayList so it is capable with an Array
    By BlueJ1 in forum Collections and Generics
    Replies: 2
    Last Post: July 8th, 2009, 05:22 PM
  5. ClassCastException in Double Linked List toString
    By Rastabot in forum Collections and Generics
    Replies: 2
    Last Post: April 24th, 2009, 11:48 AM