473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 24369
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

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

Similar topics

4
by: Jason Heyes | last post by:
Why does Foo.h need to be included in Bar.cpp for this program to compile properly? I define the FooList destructor in a separate module so that a forward declaration to Foo can be used. //...
2
by: cjl | last post by:
Hey all: I have a div of a known size, 672px X 672px. In it I will display images that are of unknown size. I have two simple functions that shrink and center the image: function shrink() {...
12
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
1
by: Betsy | last post by:
I'm trying to do some pointer manipulation and can not avoid using void * as the type declaration. I get this error message : error C2036 : 'void *' : unknown size though. Is there a compiler...
3
by: manoj.pattanaik | last post by:
Hi, I am trying to compile following piece of code (bb.cpp) using aCC (HP ANSI C++ B3910B A.03.37) compiler on HP-UX 11.23. It gives error:485 //bb.cpp -- Starts #include <iostream> using...
5
by: desktop | last post by:
I have a function that takes two pointers to an array. The first point to the first element while the other points to the last element. int nums = { 1, 2, 3, 4, 5, 7, 8, 9}; int* result; int...
3
by: cppman | last post by:
Hello, I am very new to C++ programming. I am trying to read a file with very large strings back to back, no commas or any other seperations between characters in a string and the size of the...
4
by: shapper | last post by:
Hello, I have been reading a few articles about font sizing and I get various versions. All them say to use em ... ok that is what I use. But then they differ on the text size "initialization"....
7
crystal2005
by: crystal2005 | last post by:
Hi everyone, I gonna ask how to set unlimited size of array of characters. According to the tutorial that i have found after amount of time of googling, array size should be defined when we...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.