Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Using non-integers as an array index

Reply
Thread Tools

Using non-integers as an array index

 
 
spudtheimpaler@gmail.com
Guest
Posts: n/a
 
      01-26-2006
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.

 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      01-26-2006
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Steve W. Jackson
Guest
Posts: n/a
 
      01-26-2006
In article <(E-Mail Removed). com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
Mitch
Guest
Posts: n/a
 
      01-26-2006
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']
?

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      01-26-2006

"Mitch" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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


 
Reply With Quote
 
Steve W. Jackson
Guest
Posts: n/a
 
      01-26-2006
In article <(E-Mail Removed). com>,
"Mitch" <(E-Mail Removed)> 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
 
Reply With Quote
 
Mitch
Guest
Posts: n/a
 
      01-26-2006
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

 
Reply With Quote
 
Steve W. Jackson
Guest
Posts: n/a
 
      01-26-2006
In article <hscCf.137915$6K2.24804@edtnps90>,
"Oliver Wong" <(E-Mail Removed)> wrote:

> "Mitch" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> > 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
 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      01-27-2006
"Steve W. Jackson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
[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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" camelean@shaw.ca ASP .Net 3 02-22-2011 07:06 PM
Making an array wrap, where last index + 1 = first index Shawn W_ Ruby 5 09-16-2009 02:45 PM
Templated array of templates specialized by array index npankey@gmail.com C++ 6 10-12-2008 03:20 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
problem with index.html .(page is automatically gettin redirected to index.html) karthikeyavenkat Java 2 03-17-2005 10:01 PM



Advertisments