Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Initialize array without knowing it size?

Reply
Thread Tools

Initialize array without knowing it size?

 
 
saturnlee@yahoo.com
Guest
Posts: n/a
 
      10-11-2006
I was asked to create an array without knowing its size. The size of
the array is entered by the user.

Is there any way to initialize the array without knowing its size?

 
Reply With Quote
 
 
 
 
Daniel Dyer
Guest
Posts: n/a
 
      10-11-2006
On Wed, 11 Oct 2006 14:37:58 +0100, <(E-Mail Removed)> wrote:

> I was asked to create an array without knowing its size. The size of
> the array is entered by the user.
>
> Is there any way to initialize the array without knowing its size?


No. Why not just delay initialising the array until you do know its size?

Dan.

--
Daniel Dyer
http://www.uncommons.org
 
Reply With Quote
 
 
 
 
saturnlee@yahoo.com
Guest
Posts: n/a
 
      10-11-2006
I was asked to store the number ( the user enters number of digits).
For example, if user enters 2, then store 00, 01,10,11.
If i don't initialize the size of the array, how can i store the
number and use it later?
Daniel Dyer wrote:
> On Wed, 11 Oct 2006 14:37:58 +0100, <(E-Mail Removed)> wrote:
>
> > I was asked to create an array without knowing its size. The size of
> > the array is entered by the user.
> >
> > Is there any way to initialize the array without knowing its size?

>
> No. Why not just delay initialising the array until you do know its size?
>
> Dan.
>
> --
> Daniel Dyer
> http://www.uncommons.org


 
Reply With Quote
 
Daniel Dyer
Guest
Posts: n/a
 
      10-11-2006
On Wed, 11 Oct 2006 14:50:40 +0100, <(E-Mail Removed)> wrote:

> I was asked to store the number ( the user enters number of digits).
> For example, if user enters 2, then store 00, 01,10,11.
> If i don't initialize the size of the array, how can i store the
> number and use it later?


Sorry, I'm not sure what you are trying to achieve. Do you want to
generate every binary number of a given length?

Am I right in saying that there is nothing that you have to store until
after you have got the input from the user?

Dan.

--
Daniel Dyer
http://www.uncommons.org
 
Reply With Quote
 
saturnlee@yahoo.com
Guest
Posts: n/a
 
      10-11-2006
Exactly , there is nothing to do if i don't get the user input. The
goal is to generate every possible number.
Daniel Dyer wrote:
> On Wed, 11 Oct 2006 14:50:40 +0100, <(E-Mail Removed)> wrote:
>
> > I was asked to store the number ( the user enters number of digits).
> > For example, if user enters 2, then store 00, 01,10,11.
> > If i don't initialize the size of the array, how can i store the
> > number and use it later?

>
> Sorry, I'm not sure what you are trying to achieve. Do you want to
> generate every binary number of a given length?
>
> Am I right in saying that there is nothing that you have to store until
> after you have got the input from the user?
>
> Dan.
>
> --
> Daniel Dyer
> http://www.uncommons.org


 
Reply With Quote
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      10-11-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> Exactly , there is nothing to do if i don't get the user input. The
> goal is to generate every possible number.


If the user enters a number of digits then the size of the array is 2
to the power of that number. So you do know the size.

Use size = (int) Math.pow(2, number)
 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      10-11-2006
> Daniel Dyer wrote:
>> On Wed, 11 Oct 2006 14:37:58 +0100, <(E-Mail Removed)> wrote:
>>
>>> I was asked to create an array without knowing its size. The size of
>>> the array is entered by the user.
>>>
>>> Is there any way to initialize the array without knowing its size?

>> No. Why not just delay initialising the array until you do know its size?
>>
>> Dan.
>>
>> --
>> Daniel Dyer
>> http://www.uncommons.org

>


(E-Mail Removed) wrote:
> I was asked to store the number ( the user enters number of digits).
> For example, if user enters 2, then store 00, 01,10,11.
> If i don't initialize the size of the array, how can i store the
> number and use it later?


It sounds like you want an ArrayList, rather than a raw Array.
 
Reply With Quote
 
saturnlee@yahoo.com
Guest
Posts: n/a
 
      10-11-2006
But if i write

int a[][]=[size][size];

to initialize the array size. It gives out the following error

"variable input might not have been initialized"

Tor Iver Wilhelmsen wrote:
> (E-Mail Removed) writes:
>
> > Exactly , there is nothing to do if i don't get the user input. The
> > goal is to generate every possible number.

>
> If the user enters a number of digits then the size of the array is 2
> to the power of that number. So you do know the size.
>
> Use size = (int) Math.pow(2, number)


 
Reply With Quote
 
jmcgill
Guest
Posts: n/a
 
      10-11-2006
(E-Mail Removed) wrote:
> I was asked to create an array without knowing its size. The size of
> the array is entered by the user.


You have just arrived a the threshold of needing to learn dynamic memory
allocation.
 
Reply With Quote
 
jmcgill
Guest
Posts: n/a
 
      10-11-2006
jmcgill wrote:
> (E-Mail Removed) wrote:
>> I was asked to create an array without knowing its size. The size of
>> the array is entered by the user.

>
> You have just arrived a the threshold of needing to learn dynamic memory
> allocation.


Ignore me. I thought I was replying on a C newsgroup.


You want ArrayList in Java.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
Recurse over a hash without knowing any elements (except top level) Christopher Perl 2 02-16-2004 06:04 AM
Datagrid without knowing the # of row in advance Hai Nguyen ASP .Net 0 01-15-2004 01:51 AM
Can one be a network administrator without knowing anything abt programming? john63401@yahoo.com MCSE 15 09-15-2003 05:14 PM
WebRequest - download files from directory without knowing name of file CodeMotion ASP .Net 0 07-03-2003 09:59 PM



Advertisments