Initialize array without knowing it size?

Discussion in 'Java' started by saturnlee@yahoo.com, Oct 11, 2006.

  1. Guest

    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?
     
    , Oct 11, 2006
    #1
    1. Advertising

  2. Daniel Dyer Guest

    On Wed, 11 Oct 2006 14:37:58 +0100, <> 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
     
    Daniel Dyer, Oct 11, 2006
    #2
    1. Advertising

  3. Guest

    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, <> 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
     
    , Oct 11, 2006
    #3
  4. Daniel Dyer Guest

    On Wed, 11 Oct 2006 14:50:40 +0100, <> 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
     
    Daniel Dyer, Oct 11, 2006
    #4
  5. Guest

    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, <> 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
     
    , Oct 11, 2006
    #5
  6. 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)
     
    Tor Iver Wilhelmsen, Oct 11, 2006
    #6
  7. > Daniel Dyer wrote:
    >> On Wed, 11 Oct 2006 14:37:58 +0100, <> 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

    >


    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.
     
    Jeffrey Schwab, Oct 11, 2006
    #7
  8. Guest

    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:
    > 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)
     
    , Oct 11, 2006
    #8
  9. jmcgill Guest

    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.
     
    jmcgill, Oct 11, 2006
    #9
  10. jmcgill Guest

    jmcgill wrote:
    > 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.
     
    jmcgill, Oct 11, 2006
    #10
  11. writes:

    > 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.
     
    Tor Iver Wilhelmsen, Oct 12, 2006
    #11
  12. Luke Webber Guest

    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?


    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
     
    Luke Webber, Oct 16, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher
    Replies:
    2
    Views:
    541
    Christopher
    Feb 16, 2004
  2. CodeMotion
    Replies:
    0
    Views:
    575
    CodeMotion
    Jul 3, 2003
  3. Hai Nguyen
    Replies:
    0
    Views:
    356
    Hai Nguyen
    Jan 15, 2004
  4. Nitin

    Knowing size of a data type

    Nitin, Sep 2, 2003, in forum: C Programming
    Replies:
    18
    Views:
    601
    Bob Jacobs
    Sep 13, 2003
  5. Philipp
    Replies:
    1
    Views:
    375
    Tom Hawtin
    Jun 25, 2007
Loading...

Share This Page