Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Using non-integers as an array index

Thread view: 
spudtheimpaler@gmail.com - 26 Jan 2006 21:27 GMT
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.
Chris Smith - 26 Jan 2006 21:40 GMT
> 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
[quoted text clipped - 5 lines]
> Does anyone have any recommendations of good ways of using these
> 'codes' as the index to an array of such objects.

java.util.HashMap

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Steve W. Jackson - 26 Jan 2006 21:49 GMT
> 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
[quoted text clipped - 22 lines]
>
> 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 =
Signature

Steve W. Jackson
Montgomery, Alabama

Mitch - 26 Jan 2006 22:35 GMT
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(...));

[/code]

then to put an index 1 corresponding to 'AA'

[code]

put( (int) 1, (String) 'AA' );

OR

m.put( (int) 1, (String) 'AA' );

[/code]

and then i can refer to an array as system_section['AA']
?
Oliver Wong - 26 Jan 2006 22:56 GMT
> 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
[quoted text clipped - 20 lines]
>
> 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
Mitch - 26 Jan 2006 23:07 GMT
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
Steve W. Jackson - 26 Jan 2006 23:10 GMT
> > 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
[quoted text clipped - 77 lines]
>
>     - 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 =
Signature

Steve W. Jackson
Montgomery, Alabama

Oliver Wong - 27 Jan 2006 17:05 GMT
[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
Steve W. Jackson - 26 Jan 2006 23:06 GMT
> 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
[quoted text clipped - 21 lines]
> 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 =
Signature

Steve W. Jackson
Montgomery, Alabama



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.