DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

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

zacorbul

  • Participant
  • Joined in 2006
  • *
  • Posts: 2
A java question, please help if you have the time
« on: September 11, 2007, 01:11 PM »
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

  • Artificial Idiocy
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • Cody Award
    • Participated in NANY Challenge Once
    • Coding Snacks
    • Portugal Inhabitant
    • Linux User
Re: A java question, please help if you have the time
« Reply #1 on: September 11, 2007, 03:47 PM »
You get that error because you need to specify a size to the array you create, try doing
Code
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
Re: A java question, please help if you have the time
« Reply #2 on: September 11, 2007, 04:48 PM »
nearly there ;-)
Code
  	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

  • Artificial Idiocy
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • Cody Award
    • Participated in NANY Challenge Once
    • Coding Snacks
    • Portugal Inhabitant
    • Linux User
Re: A java question, please help if you have the time
« Reply #3 on: September 11, 2007, 05:12 PM »
Right, OGroeger!

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

zacorbul

  • Participant
  • Joined in 2006
  • *
  • Posts: 2
Re: A java question, please help if you have the time
« Reply #4 on: September 12, 2007, 12:33 AM »
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
Re: A java question, please help if you have the time
« Reply #5 on: September 12, 2007, 08: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().

Original address: https://www.donationcoder.com/forum/index.php?topic=9944.0