Constant list of strings and numbers

M

Mike Mimic

Hi!

Is it possible to make a constant list of lists of strings and numbers
in Java?

I know that I could make an 2D array of Objects and store String and
Integer objects in it. But that could make a list unnecessary big
and I will need to cast all those elements to proper objects when
accessing them.

Something like:

.... = {{"string 1", 1, 2, 3}, {"string 2", 1, 2, 3}, {"string 3", 1, 2, 3}}


Mike
 
F

Fred

I don't understand what you mean by "constant list of strings" (i.e.
constant size vs. constant components). I mention this as a caveat to
this post.

Ideally though you could create a data structure to hold your component
lists. For example:

public class Thing {
String label;
List<Integer> data;
// ..... proper accessor methods, etc.
}

and then you could use a List of this data type:
List<Thing> l = new ArrayList<thing>(); // .....and so on.

If this seems like too much work even in the least, I'd just consider
not doing anything and maintaing your current solution. This, assuming
you've not profiled your app to see what the cost of preserving a "big"
array is. Especially with integer and string literals, you may be
suprised to see how inexpensive it is to do this.


If I addressed just one of your concerns I'd be surprised, as again I
didn't really understand your question all too well. So please don't
be offended if I'm restating the obvious / speaking to something
completely unrelated.
-Fred
 
B

Betty

Mike Mimic said:
Hi!

Is it possible to make a constant list of lists of strings and numbers
in Java?

I know that I could make an 2D array of Objects and store String and
Integer objects in it. But that could make a list unnecessary big
and I will need to cast all those elements to proper objects when
accessing them.

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.
 
M

Mike Mimic

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
 
L

Lasse Reichstein Nielsen

Mike Mimic said:
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.

If you want correctly, but differently, typed values in the same object,
your best bet is to roll your own class for that object.

private static MyStringNum3 {
public final String string;
public final int num1;
public final int num2;
public final int num3;
public MyStringNum3(String string, int num1, int num2, int num3) {
this.string = string;
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
}

private static final MyStringNum3[] test = {
new MyStringNum3("string 1", 1, 2, 3),
new MyStringNum3("string 2", 1, 2, 3),
new MyStringNum3("string 3", 1, 2, 3),
};

Especially because I cannot do:

if (test[0][1] == 1) {
// ... do something
}


... if (test[0].num1 == 1) { ...


Good luck.
/L
 
M

Mike Mimic

Hi!
If you want correctly, but differently, typed values in the same object,
your best bet is to roll your own class for that object.

Thanks. Nice idea. I am going to use it. It is really cleaner code.

Maybe any other ideas? (Just to be sure.)


Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top