unknown -size initialization of an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bela
    New Member
    • Sep 2007
    • 13

    unknown -size initialization of an array

    hello,

    may I know the method to create an array with unknown size?
    in C , one can go for malloc.
    what is the method in java?

    regards,
    bela
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by bela
    hello,

    may I know the method to create an array with default size?
    size is unknown to me initially.

    regards,
    bela
    I don't think, there is a default size for arrays in Java initially, but you can always do something like this:
    [CODE=java]
    int defaultSize = 10;
    int size;
    // try to get the size from somewhere, if no size is given, set size = defaultSize
    int[] array = new int[size];
    [/CODE]
    Greetings,
    Nepomuk

    Comment

    • bela
      New Member
      • Sep 2007
      • 13

      #3
      Originally posted by nepomuk
      I don't think, there is a default size for arrays in Java initially, but you can always do something like this:
      [CODE=java]
      int defaultSize = 10;
      int size;
      // try to get the size from somewhere, if no size is given, set size = defaultSize
      int[] array = new int[size];
      [/CODE]
      Greetings,
      Nepomuk
      thank you.
      I'm sorry, I want to say array of unknown size.
      in C, using malloc, we can assign memoey,
      how can we do this in java?
      regards

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by bela
        thank you.
        I'm sorry, I want to say array of unknown size.
        in C, using malloc, we can assign memoey,
        how can we do this in java?
        regards
        I'm not familiar with C, but as far as I know, C (and C++) are much less strict when it comes to arrays than Java. If you want a Collection, which you can change the size of, use a Vector. It will automatically adjust it's size to what you enter into it. Otherwise, it's not much different to an array.

        Greetings,
        Nepomuk

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by bela
          thank you.
          I'm sorry, I want to say array of unknown size.
          in C, using malloc, we can assign memoey,
          how can we do this in java?
          regards
          Use arraylist instead. Array length is fixed in java and should be specified when the array is initialized.

          Comment

          • bela
            New Member
            • Sep 2007
            • 13

            #6
            Originally posted by nepomuk
            I'm not familiar with C, but as far as I know, C (and C++) are much less strict when it comes to arrays than Java. If you want a Collection, which you can change the size of, use a Vector. It will automatically adjust it's size to what you enter into it. Otherwise, it's not much different to an array.

            Greetings,
            Nepomuk

            Thank you.
            Its working.

            regards,
            bela

            Comment

            • bela
              New Member
              • Sep 2007
              • 13

              #7
              Originally posted by r035198x
              Use arraylist instead. Array length is fixed in java and should be specified when the array is initialized.

              yes, it also working.
              thank you.
              regards,
              bela

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                I hope you googled for ArrayList vs Vector.

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  Originally posted by r035198x
                  I hope you googled for ArrayList vs Vector.
                  I have now... learned something again! :-)

                  Greetings,
                  Nepomuk

                  Comment

                  • bela
                    New Member
                    • Sep 2007
                    • 13

                    #10
                    Originally posted by r035198x
                    I hope you googled for ArrayList vs Vector.

                    Yes, me too.

                    regards,
                    bela

                    Comment

                    Working...