471,317 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 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 1239
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by lawrence | last post: by
52 posts views Thread by Jim Langston | last post: by
36 posts views Thread by pereges | last post: by

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.