Initialize array without knowing it size?

S

saturnlee

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?
 
D

Daniel Dyer

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.
 
S

saturnlee

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?
 
D

Daniel Dyer

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.
 
S

saturnlee

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

Tor Iver Wilhelmsen

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)
 
J

Jeffrey Schwab

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

Dan.

> 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.
 
S

saturnlee

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"
 
J

jmcgill

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.
 
J

jmcgill

jmcgill said:
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.
 
T

Tor Iver Wilhelmsen

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

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

"variable input might not have been initialized"

Those are not connected.

The compiler's complaint is that (in the code you did not post) you
declare the local variable "input", assign to it in a conditional (if,
for, while or even try/catch) and then try and use it after. You fix
that by assigning null or something to it when you declare it.
 
L

Luke Webber

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?

If that is really the problem, you don't need to store all the numbers
at all, just the start and end values. Why waste memory on values that
can be easily derived with a very simple calculation?

Luke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top