tags:

views:

215

answers:

6
TCPPacket TCPobj = new TCPPacket((int)obj.src_port.get(t),(int)obj.dst_port.get(t),
                                       obj.sequence.get(t),obj.ack_num.get(t),obj.urg.get(t),
                                       obj.ack.get(t),obj.psh.get(t), obj.rst.get(t),obj.syn.get(t),
                                       obj.fin.get(t),obj.rsv1.get(t),obj.rsv2.get(t),
                                       obj.window.get(t),obj.urgent_pointer.get(t));

src_port, dst_port, sequence...etc are my ArrayList but my constructor require int types. I guess i would have to cas them but don't know how?

+1  A: 

Without more context it's difficult to say, but if obj.src_port is an ArrayList containing Integer objects, you could say:

    TCPPacket TCPobj = new TCPPacket(((Integer)obj.src_port.get(t)).intValue(),
                                     ...
                                    );
);

i.e. you would first have to cast to Integer, and then unbox to an int. In Java 5 and onwards, the unboxing will be done for you.

Simon Nickerson
how can i cast if i have other types like boolean and long?
Kam
The same pattern applies, ((Boolean)obj.get(b)).booleanValue(), ((Long)obj.get(l)).longValue(), etc
Rich Seller
+1  A: 

You can't cast to int, you'll need to use Integer and then, if you must end up with an int, call .intValue(). ie:

int thisInt = ((Integer) obj.src_port.get(t)).intValue();

All the Java primitives (boolean, long, etc) have full object equivalents (Boolean, Long, etc) which can be cast to. These objects each have a method for access to the primitive (booleanValue(), longValue(), etc) which will give you the primitive equivalent.

Chris J
A: 

You need to replace each

(int)obj.XYZ.get(t)

with

((Integer)obj.XYZ.get(t)).intValue()
quosoo
how can i cast if i have other types like boolean and long...?
Kam
Well - with Long you do exactly the same i.e. ((Long)obj.XYZ.get(t)).intValue() or ((Long)obj.XYZ.get(t)).longValue() depending what type you want to convert to. Boolean though does not have int conversion method so you could try ((Boolean)obj.XYZ.get(t)).booleanValue()?1:0
quosoo
A: 

While you can cast using the method outlined by Chris J, that might be step siding the real problem.

If you have control of the obj class, GSE_packet, you want to chance you accessors (getters) to provide the type.

Right now your accessors are, obj.ack.get(t), which is, if I'm reading it correctly, bad Object Oriented design. I looks like you have object.field.get(t). You should have, object.getack(t).

So in GSE_packet, change

//this is a bad class name
// GsePacket or GSEPacket would be more in tune with java standards
class GSE_packet {
    public ack;
}

to

class GsePacket {
    private ack;

    public int getAck(what you are passing here) {
        return ack;
    }
}
James McMahon
A: 

Use toArray and reflection.

(Reflection is a really bad idea, btw, but I just wanted to say it.)

Tom Hawtin - tackline
+1  A: 

If you control the class that obj is an instance of, I strongly recommend changing those ArrayLists to be generic.

Generics work by adding casts to the bytecode for you, much like autoboxing does. The pair of them are quite useful together.

For instance, if I have this:

public List<integer> numbers = new ArrayList<Integer>();

Then I can also do this:

numbers.add(80); // Boxing

int source_port = numbers.get(0); // Casting, Unboxing

and it turns into the equivalent of this behind the scenes:

numbers.add(Integer.valueOf(80)); // Boxing

int source_port = ((Integer) numbers.get(0)).intValue(); // Casting, Unboxing

This is really contrived, but it saves a lot of manual labor.

R. Bemrose
Thanks for all above answers!!
Kam
Additional condition here is that Java 5 or 6 is in use, but that's pretty much always the case by now.
quosoo
Yeah, even a long running web app where I work has been upgraded to Java 5.
R. Bemrose