Hi!
Something like:
... = {{"string 1", 1, 2, 3}, {"string 2", 1, 2, 3}, {"string 3", 1, 2,
3}}
Your example is not right because all of the values in an array
must be of the same type.
You can have an array of arrays though. For example:
int[][] abc = new int[12][];
is an array of 12 arrays of int.
Each of the 12 arrays can have a different length.
Sorry. The example has not been full. Here is the better one. In Java
1.5 I can do (because of the autoboxing):
private static final Object[][] test = {{"string 1", 1, 2, 3}, {"string
2", 1, 2, 3}, {"string 3", 1, 2, 3}};
But I do not like this solution because I have an array of objects. I
would really like to store the integers with just primitive values.
Especially because I cannot do:
if (test[0][1] == 1) {
// ... do something
}
And I have to use:
if ((Integer)test[0][1] == 1) {
// ... do something
}
or:
if (test[0][1].equals(1)) {
// ... do something
}
I know that == performs a reference identity comparisons so that the
"test[0][1] == 1" is not a correct code in my case, but I would like to
be able to compare the object by its value with == and not by reference.
The other problem I find with this solution is that if I use those
comparisons in the loop, Java will convert that 1 to the Integer object
over and over just to compare it with the object from the array and then
to throw it away.
I have not profiled such loop to know if this really matters. I am just
uncomfortable with this.
I can still compare with:
if (((Integer)test[0][1]).intValue() == 1) {
// ... do something
}
But this is just ugly and unreadable.
The other solution I see is to use two arrays. One for strings and other
for integeres. But then keeping them synchronized ...
So and ideas?
Mike