[thelist] Java Arrays

Rob Whitener rwhitener at DesignOptions.com
Tue Mar 11 10:48:08 CST 2003


I am not sure if anyone has replied to you yet, I was just looking over the
list (to see if anyone had replied to a couple of my lists) but instead of
using an array, you may try a vector.  To import I believe it is import
java.util

http://java.sun.com/j2se/1.3/docs/api/java/util/Vector.html  actually
vectors won't let you use string keys.

A hashmap might also help:

http://www.linuxgazette.com/issue57/tindale.html  This lets you store data
and index it using a key.  However, java determines the key from the data
you are trying to store, so you can't actually assign a string index for
data.

What you may want to do is roll your own array that would allow you to do
something similar.  You probably won't ever be able to use the syntax:

my_array["index"] = value

but you could write a class that maps string values to indices between two
arrays.  
One way to do this would be to compute a simple hash from the string value
(turning it into an integer) then using this to add and access elements.
something like md5 would produce a unique hash, preventing data collisions
(where two strings hash to the same value) or you could write simpler hash
function.  I think the hash function that java uses is presented in the
linuxgazette link above.  Once you have this worked out write functions that
would allow you to add and access elements eg. (if you created a class named
StringIndexedArray):

StringIndexedArray my_array = new StringIndexedArray();
my_array.addElement("Foo",2);
temp = my_array.accessElement("Foo")

(temp would equal 2).

If you go this route, you would need to work out how this would handle multi
dimensional arrays.

Java doesn't allow operator overloading (I believe) like C++ does, otherwise
you could just write a C++ class that does this by overloading the []
operator.

Here is some documentation for arrays:

http://java.sun.com/docs/books/jls/first_edition/html/10.doc.html

Hope this helps, and isn't horribly late.

Rob

-----Original Message-----
From: Nebula [mailto:Nebula at Planet-Nebula.com]
Sent: Saturday, March 08, 2003 4:59 PM
To: thelist at lists.evolt.org
Subject: [thelist] Java Arrays


I have a few questions about how arrays work in Java.  I'm just teaching
myself it, and I'm getting pretty good, but I don't know how to work
with arrays.

First off, can you have an array key be a string?  I know in PHP, you
can do something like:

$myArray['foo'] = 'bar';

When I try to do that in Java (with "s, since Java doesn't use 's for
strings), the compiler yells at me.  I assume array keys can only be
integers.  Can you have string keys in Java?

If you cannot, then instead, how do you grab an array's key by a given
contents?  Sorting through the array one-by-one could work, but I was
hoping there was something more efficient than this.  What is the Java
equivalent function of PHP's array_search()?

I would love to be able to create a multi-dimensional array.  The first
dimension will be a string (preferrably, of course).  The 2nd will be an
integer.  The value of these will be integers.  Example:

myArray[*string*][*integer*] = *integer*;		// what I mean
if you didn't understand my explination
myArray["foo"][0] = 5;					// Actual
myArray["bar"][3] = 9;					//   Examples ;)

Thanks for your help.  Hope I got my point across clearly.


-- 
* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester 
and archives of thelist go to: http://lists.evolt.org 
Workers of the Web, evolt ! 


More information about the thelist mailing list