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

ArrayList and its Insert method

Hello Everyone,

Is there anything wrong with this block of code?

ArrayList arr = new ArrayList( 33 );

for( int i = 0; i < 33; ++i )
{
arr.Insert( i, new MyObject() );
}

It's so strange that when my web application have another thread allocating
and deallocating bit junks of memory at the same time, I occasionally got
ArgumentOutOfRangeException from the the insert method. And the states of
the variables at the exception are:
i = 31
arr.Count = 4 // this is strange, shouldn't it be 31?

If I use arr.Add method instead, it works better. Has anyone seen something
like this before or something I misunderstood about ArrayList::Insert()
method? Thanks!

-zeng

Nov 16 '05 #1
5 4805
Zeng <Ze******@hotmail.com> wrote:
Is there anything wrong with this block of code?

ArrayList arr = new ArrayList( 33 );

for( int i = 0; i < 33; ++i )
{
arr.Insert( i, new MyObject() );
}

It's so strange that when my web application have another thread allocating
and deallocating bit junks of memory at the same time, I occasionally got
ArgumentOutOfRangeException from the the insert method. And the states of
the variables at the exception are:
i = 31
arr.Count = 4 // this is strange, shouldn't it be 31?

If I use arr.Add method instead, it works better. Has anyone seen something
like this before or something I misunderstood about ArrayList::Insert()
method? Thanks!


I think the key here is that you've got another thread involved. Is the
other thread doing anything with the ArrayList?

Note that the parameter you pass into the constructor is irrelevant in
terms of insertions - it only gives an initial buffer size.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
No, the other thread doesn't do anything to the array list and the array
list is a local object doesn't get passed around at all.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Zeng <Ze******@hotmail.com> wrote:
Is there anything wrong with this block of code?

ArrayList arr = new ArrayList( 33 );

for( int i = 0; i < 33; ++i )
{
arr.Insert( i, new MyObject() );
}

It's so strange that when my web application have another thread allocating and deallocating bit junks of memory at the same time, I occasionally got ArgumentOutOfRangeException from the the insert method. And the states of the variables at the exception are:
i = 31
arr.Count = 4 // this is strange, shouldn't it be 31?

If I use arr.Add method instead, it works better. Has anyone seen something like this before or something I misunderstood about ArrayList::Insert()
method? Thanks!


I think the key here is that you've got another thread involved. Is the
other thread doing anything with the ArrayList?

Note that the parameter you pass into the constructor is irrelevant in
terms of insertions - it only gives an initial buffer size.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Zeng <Ze******@hotmail.com> wrote:
No, the other thread doesn't do anything to the array list and the array
list is a local object doesn't get passed around at all.


That sounds very odd then.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
I couldn't reproduce it in my local development environment. I only saw it
in the production server where many of our users use the site at the same
time. I have tried to force garbage collection between the Insert calls but
no luck in reproducing it although there isn't another thread involved.
Since I replaced it with Add() method, it's been working fine in production.
From what I understand there isn't a difference between Insert( i, obj) and
Add( obj ) if i = count of the array object. Could it be memory management
issue with .net, its garbage collector might collect something by mistake?
:)

By the way, I couldn't open the link you referred me to. Thanks!
-zeng
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Zeng <Ze******@hotmail.com> wrote:
No, the other thread doesn't do anything to the array list and the array
list is a local object doesn't get passed around at all.


That sounds very odd then.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
Zeng <Ze******@hotmail.com> wrote:
I couldn't reproduce it in my local development environment. I only saw it
in the production server where many of our users use the site at the same
time. I have tried to force garbage collection between the Insert calls but
no luck in reproducing it although there isn't another thread involved.
Since I replaced it with Add() method, it's been working fine in production.
From what I understand there isn't a difference between Insert( i, obj) and
Add( obj ) if i = count of the array object. Could it be memory management
issue with .net, its garbage collector might collect something by mistake?
:)
It shouldn't be. Are you positive that both i and the array list
variables are local variables?
By the way, I couldn't open the link you referred me to. Thanks!


That's odd - the link itself is okay. Try
http://www.yoda.arachsys.com/csharp/complete.html

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

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

Similar topics

7
by: William Stacey [MVP] | last post by:
I can think of a couple ways to do this, but was wonder what the fastest way you can think of. Want to take any arraylist of > 1 element and make the first element the last, and all the rest of...
4
by: Zanzibar | last post by:
H In Delphi the Search method has Two return values. 1 Bool to indicate if it is found and 1 integer to return the possition If the bool was false, the index was the possition where the item...
5
by: nadeem_far | last post by:
Hello All, I have been trying to solve the following issue and it seems like there is no solution available any where and maybe people at Microsoft should may be of some help. I am writing a...
1
by: RBCC | last post by:
I have the following structure: Structure member Dim name As String Dim phone As String End Structure Dim memberarray As New ArrayList Dim membership As member ******************** Ihave...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
3
by: Ryan Liu | last post by:
Hi, What does ArrayList.Synchronized really do for an ArrayList? Is that equal to add lock(this) for all its public methods and properties? Not just for Add()/Insert()/Remvoe()/Count, but also...
8
by: Jeroen | last post by:
I've been searching for this answer for some time now but I either can't find the correct search in this group, or the answer's not there yet :P Here's the problem: I'm specifying an interface,...
2
by: =?Utf-8?B?RHJldw==?= | last post by:
I have a current requirement to all a subsidiary to use a current legacy application and interface with my new system to inject data when appropriate. I wanted to create a web service since we are...
5
by: cmdolcet69 | last post by:
I have an arraylist of for example 1200. I want to re organize the array so that position 1 on the arraylist if actaully now position 3 and i want to add 3 position before position 3 and at the end...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.