topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 9:04 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: A java question, please help if you have the time  (Read 5343 times)

zacorbul

  • Participant
  • Joined in 2006
  • *
  • default avatar
  • Posts: 2
    • View Profile
    • Donate to Member
A java question, please help if you have the time
« on: September 11, 2007, 08:11 AM »
Hello,
I have an int[] and I would like to wrapp it in an Integer[].
I know that
Integer convertInt = int aNumber;   
But I am not sure how I would go about it for an array.
I tried using a for loop such as:
for(int i=0; i<aNumber.length; i++)
 convertInt = aNumber

but is no use as I get an error (array dimension missing)while declaring
Integer[] convertInt = new Integer[];

If anybody could help me get back on track it would be great.
 Thank you

jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: A java question, please help if you have the time
« Reply #1 on: September 11, 2007, 10:47 AM »
You get that error because you need to specify a size to the array you create, try doing
Integer[] convertInt = new Integer[intArray.size()](0);

I'm not absolutelly sure if that's the syntax and currently i don't have eclipse installed to confirm.

OGroeger

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 76
    • View Profile
    • Donate to Member
Re: A java question, please help if you have the time
« Reply #2 on: September 11, 2007, 11:48 AM »
nearly there ;-)
  int[] ints = new int[]{1,2,3,4,5};
  Integer[] intObjects = new Integer[ ints.length ];
  for( int i = 0; i < ints.length; ++i )
  {
  intObjects[ i ] = new Integer( ints[ i ] );
  }

jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: A java question, please help if you have the time
« Reply #3 on: September 11, 2007, 12:12 PM »
Right, OGroeger!

(and i'm the one who loves java  :huh:)

zacorbul

  • Participant
  • Joined in 2006
  • *
  • default avatar
  • Posts: 2
    • View Profile
    • Donate to Member
Re: A java question, please help if you have the time
« Reply #4 on: September 11, 2007, 07:33 PM »
Thank you very much for answering all and especially OGroeger, it was spot on, it worked like a charm(actually better)  :Thmbsup:

Now I can finally finish my method. ;D
Thank you guys, I owe you a pint of lager :)

Cheers

OGroeger

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 76
    • View Profile
    • Donate to Member
Re: A java question, please help if you have the time
« Reply #5 on: September 12, 2007, 03:09 AM »
Btw, there a lot of useful classes which can be very helpful. What i use very much is the Apache commons project, in particular the lang project (JavaDoc) which contains "Utils" packages, e.g. ArrayUtils which has a toObject() method that converts an int[] to Integer[].
 
The package that i use on every Project (when possible) is StringUtils. I coundn't live without StringUtils.defaultString() and StringUtils.isBlank().