473,394 Members | 1,721 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,394 software developers and data experts.

Why can't a new row be added to an array as needed?

I realize that arrays have worked this way for ages, but why do we have
to continue with the old ways?

It would really be nice if you could add a new row (element) to an array
one at a time (as required).

In some cases, the programmer doesn't know how many objects that will be
loaded into an array until runtime. I know you can process the source to
find out how large it is and use this to update a variable that was used
to initialize the array. However, this seem's rather inefficient to me.

Why not create a new row in an array (using "new") just before an
assignment when you are inside a loop (e.g., when you are reading items
from a file line by line)?

This can be done for the creation of other objects. Why can't it be the
same for arrays?

This would be much more intuitive.

Comments?

Mike


*** Sent via Developersdex http://www.developersdex.com ***
Dec 28 '05 #1
7 1322
Mike,

This can easily be handled by using an ArrayList.

Doug
"MikeB" <no****@devdex.com> wrote in message
news:OM***************@TK2MSFTNGP15.phx.gbl...
I realize that arrays have worked this way for ages, but why do we have
to continue with the old ways?

It would really be nice if you could add a new row (element) to an array
one at a time (as required).

In some cases, the programmer doesn't know how many objects that will be
loaded into an array until runtime. I know you can process the source to
find out how large it is and use this to update a variable that was used
to initialize the array. However, this seem's rather inefficient to me.

Why not create a new row in an array (using "new") just before an
assignment when you are inside a loop (e.g., when you are reading items
from a file line by line)?

This can be done for the creation of other objects. Why can't it be the
same for arrays?

This would be much more intuitive.

Comments?

Mike


*** Sent via Developersdex http://www.developersdex.com ***

Dec 28 '05 #2
Because there is overhead in maintaining a dynamic array. The object has to
be able to recalculate the end of the array and move objects around. Also,
arrays are special types in the CLR and therefore probably needs to have
some consistent behavior.

Having said that, this is why the ArrayList was invented.

"MikeB" <no****@devdex.com> wrote in message
news:OM***************@TK2MSFTNGP15.phx.gbl...
I realize that arrays have worked this way for ages, but why do we have
to continue with the old ways?

It would really be nice if you could add a new row (element) to an array
one at a time (as required).

In some cases, the programmer doesn't know how many objects that will be
loaded into an array until runtime. I know you can process the source to
find out how large it is and use this to update a variable that was used
to initialize the array. However, this seem's rather inefficient to me.

Why not create a new row in an array (using "new") just before an
assignment when you are inside a loop (e.g., when you are reading items
from a file line by line)?

This can be done for the creation of other objects. Why can't it be the
same for arrays?

This would be much more intuitive.

Comments?

Mike


*** Sent via Developersdex http://www.developersdex.com ***

Dec 28 '05 #3
Beat me to it :)

It's what the ArrayList was designed for, well partially.

Mark

"Doug Handler" <dk*******@yahoo.com> wrote in message
news:O2****************@TK2MSFTNGP11.phx.gbl...
Mike,

This can easily be handled by using an ArrayList.

Doug
"MikeB" <no****@devdex.com> wrote in message
news:OM***************@TK2MSFTNGP15.phx.gbl...
I realize that arrays have worked this way for ages, but why do we have
to continue with the old ways?

It would really be nice if you could add a new row (element) to an array
one at a time (as required).

In some cases, the programmer doesn't know how many objects that will be
loaded into an array until runtime. I know you can process the source to
find out how large it is and use this to update a variable that was used
to initialize the array. However, this seem's rather inefficient to me.

Why not create a new row in an array (using "new") just before an
assignment when you are inside a loop (e.g., when you are reading items
from a file line by line)?

This can be done for the creation of other objects. Why can't it be the
same for arrays?

This would be much more intuitive.

Comments?

Mike


*** Sent via Developersdex http://www.developersdex.com ***


Dec 28 '05 #4
The overhead argument makes sense and as a result it makes sense that
the ArrayList mechanism exists in addition to the standard method.

But I don't think objects would have to be moved around as you have
suggested: What could be done is just change pointers or add new ones.
In this way new rows/elements could be added and they wouldn't have to
be contiguous. But, you are absolutely correct there is additional
overhead and for those embedded systems programmers this might be a
significant issue.

Thanks very much for all the responses.

*** Sent via Developersdex http://www.developersdex.com ***
Jan 6 '06 #5
MikeB <no****@devdex.com> wrote:
The overhead argument makes sense and as a result it makes sense that
the ArrayList mechanism exists in addition to the standard method.

But I don't think objects would have to be moved around as you have
suggested: What could be done is just change pointers or add new ones.
In this way new rows/elements could be added and they wouldn't have to
be contiguous. But, you are absolutely correct there is additional
overhead and for those embedded systems programmers this might be a
significant issue.


An array is by definition a contiguous block of memory. That's what
makes it fast to access. Are you suggesting that an array should
effectively be a linked list of nodes, each of which contains part of
the array?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 6 '06 #6

"MikeB" <no****@devdex.com> wrote in message
news:u8**************@TK2MSFTNGP14.phx.gbl...
| The overhead argument makes sense and as a result it makes sense that
| the ArrayList mechanism exists in addition to the standard method.
|
| But I don't think objects would have to be moved around as you have
| suggested: What could be done is just change pointers or add new ones.
| In this way new rows/elements could be added and they wouldn't have to
| be contiguous. But, you are absolutely correct there is additional
| overhead and for those embedded systems programmers this might be a
| significant issue.
|
| Thanks very much for all the responses.
|
|
|
| *** Sent via Developersdex http://www.developersdex.com ***

Not at all, Arrays have to be contiguous, how would you apply indexing when
it wasn't the case?
Each time you extend and ArrayList the underlying array must be copied to a
new Array, what's done to reduce the overhead is to create a new array with
a size that is the double of the current array, that means that ArrayLists
grow exponentially.

Willy.
Jan 6 '06 #7

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:%2*****************@TK2MSFTNGP12.phx.gbl...
<snip>
Not at all, Arrays have to be contiguous, how would you apply indexing when
it wasn't the case?


Indexing makes perfectly good sense for a Linked list.
You simply need to accept that indexed access will take linear time.

Bill
Jan 7 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what...
4
by: Yuk Cheng | last post by:
<<<start index.htm>>> <html> <head> <script> function perform(action){ } </script> </head>
15
by: lawrence | last post by:
Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone...
52
by: Jim Langston | last post by:
I wanted to do an operator override for but couldnt' figure out the syntax. I tried this (code that doesn't compile commented out with //: class CMyBitmap { public: CMyBitmap( int Rows, int...
7
by: runsun pan | last post by:
I wanna check if an object is the *arguments* object of a function, is that possible? When we do: typeof(arguments) it always returns "object", doesn't seem to be helpful.
6
by: silverburgh.meryl | last post by:
Hi, In one A.cpp file, I have defined a static array of JSFunctionSpec, like this: static JSFunctionSpec JProfFunctions = { {"JProfStartProfiling", JProfStartProfiling, 0, 0, 0 },...
14
by: dan | last post by:
I would like to have the preprocessor automatically generate the number of array elements requested. Each element is zero. The elements get pasted into a larger array. The other elements may be...
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
13
by: Just_a_fan | last post by:
I am adding a bunch of controls with the code below. Problem 1: When program flow passes to "UpperChanged" when I click it, the control name is undefined. When I enter: If udUpperLim1.Value 1...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.