Results 1 to 3 of 3
- 08-15-2008, 02:45 AM #1
Member
- Join Date
- Aug 2008
- Posts
- 3
- Rep Power
- 0
Yet another Wrapper Class confused guy.
Java is a pretty much new language to me and I'm still in the early learning process of it, I had myself making some pratice with Vector objects and of curse Wrapper Classes so I could use native variables with it but it looks like that those guides I'm reading from didn't clear my mind about it so much.
So I filled this Vector (data) with a few int numbers inside a setMethod and trying now to pull them out in a getMethod to operate with them; I basically tried to use a for iteration to pick each element and assign it to a Integer variable, and then to revert the number to it's native int type using the intValue() method. The original thing was pretty much like this:
which obviously didn't work.Java Code:int pre=0; for (int i=0; i<data.size();i++){ Integer num = new Integer ( data.get(i) ); per+=num.intValue(); }
Googling around I somehow managed to accomplish it but I'm not sure if I found the best way to do it so I'd like to be sure about it before moving on:
So I pull my data out in a string using it's .valueOf before I could convert and treat it as an int. Now the n00b question is, was there a most direct way to accomplish it?Java Code:int pre=0; for (int i=0; i<data.size();i++){ String [COLOR="Sienna"]buf[/COLOR] = String.valueOf(data.get(i)); Integer num = new Integer([COLOR="Sienna"]buf[/COLOR]); per+=num.intValue(); }
- 08-15-2008, 03:22 AM #2
Vectors only hold objects not primitives. If you have a young version of java, the compiler will convert int to Integer for you (boxing). So your Vector holds Integer objects. You can get them out by casting the object returned by get(i) :filled this Vector (data) with a few int numbers
num = (Integer)get(i);
Given the Integer object, you can easily get its value. In fact the compiler will do the work for you (boxing again): per += num;
Also there are Generics that allow you to specify the type that Vector will hold, allows the compiler to type check adds and to do casting for you on gets.
- 08-15-2008, 04:04 AM #3
Member
- Join Date
- Aug 2008
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Secure FTP Wrapper 3.0.3
By Java Tip in forum Java SoftwareReplies: 0Last Post: 07-23-2008, 01:56 PM -
Wrapper Class
By haiforhussain in forum New To JavaReplies: 6Last Post: 06-24-2008, 08:08 AM -
problem in wrapper class
By binoympappachen in forum New To JavaReplies: 4Last Post: 12-13-2007, 12:31 PM -
javax.servlet.ServletException: Wrapper cannot find servlet class util.t2
By osval in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 03:47 PM -
Exception Failed to Generate Wrapper Class on WebLogic
By christina in forum New To JavaReplies: 1Last Post: 08-07-2007, 02:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks