views:

252

answers:

6

As a follow up to my question about j2me dynamic arrays,

I'm now trying to figure out a way to change the values of the Integers in my Vector.

Say I have a Vector v, and Array arr, and ints x, y and i;

In c++ I could do:

v[arr[x][y]] += i;

In j2me the best way I found so far to do the same is:

v.setElementAt(new Integer(((Integer)(v.elementAt(arr[x][y]))).intValue()+i), arr[x][y]);

Is this really the best way to do it j2me?

If it is, what went wrong here? Java is supposed to make me "do less work" and "do things for me" yet I find myself again and again doing extra work for it. Is something wrong with me, or is it some problem with Java?

Edit: I'm using the J2me SDK 3.0 which looks like it is Java 1.3 so no fancy generics and auto boxing and all that stuff.

A: 
Vector<Integer> v = new Vector<Integer>(); 
v.setElementAt( arr[x][y], arr[x][y] );
Steve Claridge
He's using J2ME and it doesn't have generics
Vanya
A: 

EDIT: While answering this question, I did not know that J2ME does not support Generics. Thanks to SO for teaching me that :-)

EDIT 2: My solution is wrong since J2ME does not support Generics.

If you have used Generics (JDK 1.5 onwards), you could do it simpler!

If I assume the declaration to be thus,

Vector<Integer> v = new Vector<Integer>();

then

v.setElementAt(new Integer(((Integer)(v.elementAt(arr[x][y]))).intValue()+i), arr[x][y]);

becomes

v.setElementAt(new Integer(v.elementAt(arr[x][y]).intValue()+i), arr[x][y]);
Vijay Dev
This question is apparently about J2ME, not regular Java. Good point tho'.
Esko
Oh! So J2ME does not support Generics? Never knew that.
Vijay Dev
Yeah, me neither; quite surprised by it too!
Steve Claridge
+3  A: 

I'm afraid that's how it is in ME, although I'd split it to avoid that hairy oneliner:

Integer val = (Integer)v.elementAt(arr[x][y]);
int newVal = val.intValue() + i;
v.setElementAt(new Integer(newVal), arr[x][y]);

Stuff got a lot better with autoboxing and generics, but they came in Java 5 and J2ME is basically a stripped version of Java 1.3 unless I've been misinformed. Here's how it looks in Java 5+:

v.setElementAt(arr[x][y], v.get(arr[x][y]) + i);

Still more verbose than C++, but at least without the casting. I understand there was reluctance to add generics and such to Java as it might be "too hard" for the average programmer to understand [Citation needed]. And so we ended up with unreadable code until .Net got generics and Sun jumped on the bandwagon.

Anywho, I agree the collections framework was a pain to use before generics/boxing, but I hope at least you'll enjoy not having to debug broken pointers and corrupted memory.

gustafc
Generics *is* too hard even for good programmers to understand, if you look at its more obscure interactions with other language features such as inheritance and autoboxing. But that's irrelevant for most daily usage. Of course, as a language designer, it's the first thing you think about, so maybe that's an explanation for the reluctance.
Michael Borgwardt
+1  A: 

Java SE has had some changes to the language (Generics) that would make this code a bit simpler, for ME I'd guess you are out of luck.

I would go for the suggested solution of creating your own class that wraps a plain array (and allocates a bigger one when needed) that was given as an answer to your previous question.

Simon Groenewolt
A: 

I don't see the reason why you wouldn't do it the same way as you do in C++. OK, you have to implement the dynamically scaling array container yourself but if you do, you get rid of the Integer issue where Java actually creates a new Object of type Integer instead of int primitive type.

Your original question has a nice example of a dynamic int primitive type array as an answer, go check it out again.

Esko
+1  A: 

You have two things here that are conspiring to bloat the code: Lack of a typesafe collection, and an immutable int wrapper.

One solution would be to use a typesafe collection. GNU Trove has TIntArrayList for this:

v.set(arr[x][y], v.get(arr[x][y]) + i);

Alternatively, you can use a mutable class like org.jboss.util.MuInteger:

((MuInteger)v.elementAt(arr[x][y])).add(i);

Or, as a dirty hack, arrays of length 1:

((int[])v.elementAt(arr[x][y]))[0] += i;

If you can combine both (would definitely require you to write a a custom collection class, in the absence of Generics):

v.get(arr[x][y]).add(i);
Michael Borgwardt