469,330 Members | 1,379 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

unknown -size initialization of an array

13
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
Sep 20 '07 #1
9 24038
Nepomuk
3,112 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. int defaultSize = 10;
  2. int size;
  3. // try to get the size from somewhere, if no size is given, set size = defaultSize
  4. int[] array = new int[size];
  5.  
Greetings,
Nepomuk
Sep 20 '07 #2
bela
13
I don't think, there is a default size for arrays in Java initially, but you can always do something like this:
Expand|Select|Wrap|Line Numbers
  1. int defaultSize = 10;
  2. int size;
  3. // try to get the size from somewhere, if no size is given, set size = defaultSize
  4. int[] array = new int[size];
  5.  
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
Sep 20 '07 #3
Nepomuk
3,112 Expert 2GB
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
Sep 20 '07 #4
r035198x
13,262 8TB
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.
Sep 20 '07 #5
bela
13
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
Sep 20 '07 #6
bela
13
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
Sep 20 '07 #7
r035198x
13,262 8TB
I hope you googled for ArrayList vs Vector.
Sep 20 '07 #8
Nepomuk
3,112 Expert 2GB
I hope you googled for ArrayList vs Vector.
I have now... learned something again! :-)

Greetings,
Nepomuk
Sep 20 '07 #9
bela
13
I hope you googled for ArrayList vs Vector.

Yes, me too.

regards,
bela
Sep 21 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Jason Heyes | last post: by
12 posts views Thread by manochavishal | last post: by
1 post views Thread by Betsy | last post: by
5 posts views Thread by desktop | last post: by
4 posts views Thread by shapper | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.