Using non-integers as an array index

Discussion in 'Java' started by spudtheimpaler@gmail.com, Jan 26, 2006.

  1. Guest

    Hi there. I have an array of objects that i will be referring to in my
    program, they are sections of a transport system. In the system I am
    simulating historically and presently they are labelled alphabetically
    of the form AAA or CBA. I am simulating a smaller section and will
    currently only be using two characters. Say AA - AZ for one set of
    sections, BA - BR for a second, smaller set etc...

    Does anyone have any recommendations of good ways of using these
    'codes' as the index to an array of such objects. As far as i know
    using Java you can only use integers as an index to an array but I may
    be mistaken (I think in php and others you can index aray['aa']). Is
    there perhaps a way to use integers as the index but miss numbers, or
    not do them in order (EG perhaps the letters could be the hex
    interpretation of an integer index - though then the first index would
    be at 170, and then if i used AAA it would be 2730) if i used this
    method how much memory /resources would the empty objects take up (as
    in would the empty objects still take up memory or is there a method
    for leaving them empty / null and thus taking no / little memory)

    I hope I have been clear in what i am looking for, i realise this is
    more looking for ideas than help with code but I hope you can still
    help.

    Kind Regards,

    Mitch.
    , Jan 26, 2006
    #1
    1. Advertising

  2. Chris Smith Guest

    <> wrote:
    > Hi there. I have an array of objects that i will be referring to in my
    > program, they are sections of a transport system. In the system I am
    > simulating historically and presently they are labelled alphabetically
    > of the form AAA or CBA. I am simulating a smaller section and will
    > currently only be using two characters. Say AA - AZ for one set of
    > sections, BA - BR for a second, smaller set etc...
    >
    > Does anyone have any recommendations of good ways of using these
    > 'codes' as the index to an array of such objects.


    java.util.HashMap

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jan 26, 2006
    #2
    1. Advertising

  3. In article <>,
    wrote:

    > Hi there. I have an array of objects that i will be referring to in my
    > program, they are sections of a transport system. In the system I am
    > simulating historically and presently they are labelled alphabetically
    > of the form AAA or CBA. I am simulating a smaller section and will
    > currently only be using two characters. Say AA - AZ for one set of
    > sections, BA - BR for a second, smaller set etc...
    >
    > Does anyone have any recommendations of good ways of using these
    > 'codes' as the index to an array of such objects. As far as i know
    > using Java you can only use integers as an index to an array but I may
    > be mistaken (I think in php and others you can index aray['aa']). Is
    > there perhaps a way to use integers as the index but miss numbers, or
    > not do them in order (EG perhaps the letters could be the hex
    > interpretation of an integer index - though then the first index would
    > be at 170, and then if i used AAA it would be 2730) if i used this
    > method how much memory /resources would the empty objects take up (as
    > in would the empty objects still take up memory or is there a method
    > for leaving them empty / null and thus taking no / little memory)
    >
    > I hope I have been clear in what i am looking for, i realise this is
    > more looking for ideas than help with code but I hope you can still
    > help.
    >
    > Kind Regards,
    >
    > Mitch.


    If you can be reasonably certain that you'll never need to mix the
    lengths of your "index" values, you might consider something like a
    TreeMap. It's similar to other Map implementations in that it lets you
    store objects keyed by other objects -- your "index" values as String
    objects. (Mixing two- and three-character keys means that lexical
    sorting issues come into play, if that matters.) Any of the various
    operations to get a key set, collection, etc., would then be in the
    natural order of those strings.

    = Steve =
    --
    Steve W. Jackson
    Montgomery, Alabama
    Steve W. Jackson, Jan 26, 2006
    #3
  4. Mitch Guest

    Thank you, both of those appear to be what I am looking for. I've not
    come across a map object before so the documentation is somewhat
    confusing. Is a map basically a table with an integer index and a key
    realting to it. For example i would create a new map M:

    Code:
    
    Map m = Collections.synchronizedMap(new TreeMap(...));
    
    
    then to put an index 1 corresponding to 'AA'

    Code:
    
    put( (int) 1, (String) 'AA' );
    
    OR
    
    m.put( (int) 1, (String) 'AA' );
    
    
    and then i can refer to an array as system_section['AA']
    ?
    Mitch, Jan 26, 2006
    #4
  5. Oliver Wong Guest

    "Mitch" <> wrote in message
    news:...
    > Thank you, both of those appear to be what I am looking for. I've not
    > come across a map object before so the documentation is somewhat
    > confusing. Is a map basically a table with an integer index and a key
    > realting to it. For example i would create a new map M:
    >
    >
    Code:
    >
    > Map m = Collections.synchronizedMap(new TreeMap(...));
    >
    > 
    >
    > then to put an index 1 corresponding to 'AA'
    >
    >
    Code:
    >
    > put( (int) 1, (String) 'AA' );
    >
    > OR
    >
    > m.put( (int) 1, (String) 'AA' );
    >
    > 
    >
    > and then i can refer to an array as system_section['AA']


    If you have a special need for some special order (i.e. not
    lexigraphical order) of the two letter codes to be maintained, use a
    LinkedListMap instead of a TreeMap.

    Use the two letter codes as the keys in the map, and the objects you're
    mapping to as the values.

    <PHP code>
    $myMap['AA'] = myObject1;
    $myMap['AB'] = myObject2;
    $myMap['CZ'] = myObject3;
    ....

    //Do something will all the objects
    foreach($myMap as $key => $value) {
    doSomething($value);
    }

    //Do something with only object CZ
    doSomething($myMap['CZ']);
    </PHP code>

    <Java 1.4 code>
    Map myMap = new LinkedListMap(); /*Synchronize this if you want*/
    myMap.put("AA", myObject1);
    myMap.put("AB", myObject1);
    myMap.put("CZ", myObject1);
    ....
    //Do something will all the objects
    Iterator valueIterator = myMap.valueSet().iterator();
    while (valueIterator.hasNext()) {
    MyObjectClass moc = (MyObjectClass)valueIterator.next();
    moc.doSomething();
    }
    //Do something with only object CZ
    MyObjectClass moc = (MyObjectClass)myMap.get("CZ");
    moc.doSomething();
    </Java 1.4 code>

    <Java 1.5 code>
    Map<String,MyObjectClass> myMap = new LinkedListMap<String,MyObjectClass>();
    /*Synchronize this if you want*/
    myMap.put("AA", myObject1);
    myMap.put("AB", myObject1);
    myMap.put("CZ", myObject1);
    ....
    //Do something will all the objects
    for (MyObjectClass moc : myMap.valueSet()) {
    moc.doSomething();
    }

    //Do something with only object CZ
    myMap.get("CZ").doSomething();
    </Java 1.5 code>

    - Oliver
    Oliver Wong, Jan 26, 2006
    #5
  6. In article <>,
    "Mitch" <> wrote:

    > Thank you, both of those appear to be what I am looking for. I've not
    > come across a map object before so the documentation is somewhat
    > confusing. Is a map basically a table with an integer index and a key
    > realting to it. For example i would create a new map M:
    >
    >
    Code:
    >
    > Map m = Collections.synchronizedMap(new TreeMap(...));
    >
    > 
    >
    > then to put an index 1 corresponding to 'AA'
    >
    >
    Code:
    >
    > put( (int) 1, (String) 'AA' );
    >
    > OR
    >
    > m.put( (int) 1, (String) 'AA' );
    >
    > 
    >
    > and then i can refer to an array as system_section['AA']
    > ?


    I haven't yet moved to Java 1.5 and generics, so I'll couch responses in
    terms of 1.4.2 and earlier.

    I used the term "index" in an effort to make it familiar to your
    original inquiry about an array. But there's not really an index --
    there's not an array. A map is a collection of key-value pairs. There
    are objects serving as entries in a map and there are other objects
    which are the keys used to find each particular entry.

    Your original inquiry referred to some languages supporting associative
    arrays, and with a map you could indeed use "AA" much like an array
    index reference. That's what I was attempting to describe in my reply.
    I envisioned that you wanted an array something like this:

    [AA] --> someObject1
    [AB] --> someObject2
    [AC] --> someObject3
    [BA] --> someObject4
    [BB] --> someObject5

    So the map would take calls like this:

    put("AA", someObject1);
    put("AB", someObject2);
    put("AC", someObject3);

    And so on... Each such call specifies an object that will serve as a
    key, and an object which will be the value.

    To get a specific object, you could use get("AA"). It could be null if
    you never added anything using "AA" as a key, or if you actually made a
    null entry. If you're using Java 1.5, with generics, you can specify
    the type at compile time that you'll put into your map, whereas 1.4.2
    and earlier will return an Object and you'll have to cast it to whatever
    type it really is. Unlike an array, though, there's not an "empty spot"
    if you don't provide, for instance, a value keyed to "CC" -- it's simply
    not there.

    I mentioned the TreeMap only because it allows you to iterate over the
    contents and have them come out in the order of the keys. If you don't
    need to iterate, or don't care about the order in which they come out
    during iteration, a HashMap would be better.

    Hope this helps clarify.

    = Steve =
    --
    Steve W. Jackson
    Montgomery, Alabama
    Steve W. Jackson, Jan 26, 2006
    #6
  7. Mitch Guest

    Thank you! That has clarified it completely! Not to say that I won't be
    back, but I'm starting off on a much stronger setting.

    Really thats perfect. They do have to be in order as such, but I am
    thinking of using a linked list from within the object Eg AA goes to AB
    but AB can go to AC OR BX and so these junctions have to be considered.
    I will investigate any attributes the this LinkedListMap to see if
    this is feasible another way.

    Kind Regards and thanks again!

    Mitch
    Mitch, Jan 26, 2006
    #7
  8. In article <hscCf.137915$6K2.24804@edtnps90>,
    "Oliver Wong" <> wrote:

    > "Mitch" <> wrote in message
    > news:...
    > > Thank you, both of those appear to be what I am looking for. I've not
    > > come across a map object before so the documentation is somewhat
    > > confusing. Is a map basically a table with an integer index and a key
    > > realting to it. For example i would create a new map M:
    > >
    > >
    Code:
    > >
    > > Map m = Collections.synchronizedMap(new TreeMap(...));
    > >
    > > 
    > >
    > > then to put an index 1 corresponding to 'AA'
    > >
    > >
    Code:
    > >
    > > put( (int) 1, (String) 'AA' );
    > >
    > > OR
    > >
    > > m.put( (int) 1, (String) 'AA' );
    > >
    > > 
    > >
    > > and then i can refer to an array as system_section['AA']

    >
    > If you have a special need for some special order (i.e. not
    > lexigraphical order) of the two letter codes to be maintained, use a
    > LinkedListMap instead of a TreeMap.
    >
    > Use the two letter codes as the keys in the map, and the objects you're
    > mapping to as the values.
    >
    > <PHP code>
    > $myMap['AA'] = myObject1;
    > $myMap['AB'] = myObject2;
    > $myMap['CZ'] = myObject3;
    > ...
    >
    > //Do something will all the objects
    > foreach($myMap as $key => $value) {
    > doSomething($value);
    > }
    >
    > //Do something with only object CZ
    > doSomething($myMap['CZ']);
    > </PHP code>
    >
    > <Java 1.4 code>
    > Map myMap = new LinkedListMap(); /*Synchronize this if you want*/
    > myMap.put("AA", myObject1);
    > myMap.put("AB", myObject1);
    > myMap.put("CZ", myObject1);
    > ...
    > //Do something will all the objects
    > Iterator valueIterator = myMap.valueSet().iterator();
    > while (valueIterator.hasNext()) {
    > MyObjectClass moc = (MyObjectClass)valueIterator.next();
    > moc.doSomething();
    > }
    > //Do something with only object CZ
    > MyObjectClass moc = (MyObjectClass)myMap.get("CZ");
    > moc.doSomething();
    > </Java 1.4 code>
    >
    > <Java 1.5 code>
    > Map<String,MyObjectClass> myMap = new LinkedListMap<String,MyObjectClass>();
    > /*Synchronize this if you want*/
    > myMap.put("AA", myObject1);
    > myMap.put("AB", myObject1);
    > myMap.put("CZ", myObject1);
    > ...
    > //Do something will all the objects
    > for (MyObjectClass moc : myMap.valueSet()) {
    > moc.doSomething();
    > }
    >
    > //Do something with only object CZ
    > myMap.get("CZ").doSomething();
    > </Java 1.5 code>
    >
    > - Oliver


    I'm guessing you mean LinkedHashMap -- unless there's a LinkedListMap in
    1.5.

    I don't have any familiarity with PHP, but I recognize the concept there
    as similar to some other areas I've used. With any luck, this will help
    the OP to get a clear picture of how to solve his problem.

    = Steve =
    --
    Steve W. Jackson
    Montgomery, Alabama
    Steve W. Jackson, Jan 26, 2006
    #8
  9. Oliver Wong Guest

    "Steve W. Jackson" <> wrote in message
    news:...
    [my code snipped]
    >
    > I'm guessing you mean LinkedHashMap -- unless there's a LinkedListMap in
    > 1.5.


    Yes, I meant LinkedHashMap. I should have put a disclaimer saying the
    (pseudo-)code was not compiled, not tested, etc.

    - Oliver
    Oliver Wong, Jan 27, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Royan
    Replies:
    32
    Views:
    989
  2. Rob Meade

    Array Integers vs Array Variables

    Rob Meade, Feb 8, 2004, in forum: ASP General
    Replies:
    16
    Views:
    245
    Roland Hall
    Feb 8, 2004
  3. Josselin
    Replies:
    22
    Views:
    294
    Phrogz
    Nov 28, 2006
  4. Shawn W_
    Replies:
    5
    Views:
    273
    Aldric Giacomoni
    Sep 16, 2009
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    280
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page