Results 1 to 3 of 3
  1. #1
    JAdeline is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Default 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:
    Java Code:
    int pre=0;
    for (int i=0; i<data.size();i++){
    			Integer num = new Integer ( data.get(i) );
    			per+=num.intValue();
    		}
    which obviously didn't work.

    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:
    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();
    		}
    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?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    filled this Vector (data) with a few int numbers
    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) :
    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.

  3. #3
    JAdeline is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Default

    And that clears up my mess, thanks :)
    Last edited by JAdeline; 08-15-2008 at 04:08 AM.

Similar Threads

  1. Secure FTP Wrapper 3.0.3
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 07-23-2008, 01:56 PM
  2. Wrapper Class
    By haiforhussain in forum New To Java
    Replies: 6
    Last Post: 06-24-2008, 08:08 AM
  3. problem in wrapper class
    By binoympappachen in forum New To Java
    Replies: 4
    Last Post: 12-13-2007, 12:31 PM
  4. Replies: 1
    Last Post: 08-07-2007, 03:47 PM
  5. Exception Failed to Generate Wrapper Class on WebLogic
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 02:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •