[thelist] Java Arrays

Nebula Nebula at Planet-Nebula.com
Sat Mar 8 15:57:01 CST 2003


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.





More information about the thelist mailing list