473,396 Members | 1,784 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.

Using an array list

Ant
Hi, I'm using an array list as the collection for my indexer (_alPeople).
When i try to set _alpeople to a value at a certain index, I get an error. I
can't simply add the value as it needs to be at an index set by the user.

the Set statement is this:

_people[index] = value; or even _people.Insert(index,value);

but these throw 'ArgumentOutOfRange' exception stating: Must be non-negative
and less than the size of the collection.

How can I use an array list & keep track of the index the user married to
the value?

Thanks for any tips on this

Ant
Oct 25 '06 #1
3 3556

As the exception name suggests, it could be that index is out of range?
Is the Hashtable class something you are looking for?

Ant wrote:
Hi, I'm using an array list as the collection for my indexer (_alPeople).
When i try to set _alpeople to a value at a certain index, I get an error. I
can't simply add the value as it needs to be at an index set by the user.

the Set statement is this:

_people[index] = value; or even _people.Insert(index,value);

but these throw 'ArgumentOutOfRange' exception stating: Must be non-negative
and less than the size of the collection.

How can I use an array list & keep track of the index the user married to
the value?

Thanks for any tips on this

Ant
Oct 25 '06 #2
KH
Ant, it sounds like you maybe need to use a Hashtable (version 1.x) -- or a
Dictionary (version 2.0) -- as it sounds like your users are defining the key
into the collection.

The array list fails because you're trying to set an index that it doesn't
contain -- say you create an ArrayList with capacity 10, and add 3 elements
to it -- its length is 3 and its index range is 0-2 inclusive. If you try to
set index 4 if fails because 4 is not between in 0-2, even though it has
capacity 10 (setting capacity just pre-allocates memory, a performance
enhancement).

Using a hashtable will allow you to maintain a list of key/value pairs where
the key is unique in the set and need not be sequential with other keys,
unlike an index.

Good luck! - Ken
"Ant" wrote:
Hi, I'm using an array list as the collection for my indexer (_alPeople).
When i try to set _alpeople to a value at a certain index, I get an error. I
can't simply add the value as it needs to be at an index set by the user.

the Set statement is this:

_people[index] = value; or even _people.Insert(index,value);

but these throw 'ArgumentOutOfRange' exception stating: Must be non-negative
and less than the size of the collection.

How can I use an array list & keep track of the index the user married to
the value?

Thanks for any tips on this

Ant

Oct 25 '06 #3
On Wed, 25 Oct 2006 07:24:38 +0200, Truong Hong Thi <th*****@gmail.com
wrote:
>
As the exception name suggests, it could be that index is out of range?
Is the Hashtable class something you are looking for?

Ant wrote:
>Hi, I'm using an array list as the collection for my indexer
(_alPeople).
When i try to set _alpeople to a value at a certain index, I get an
error. I
can't simply add the value as it needs to be at an index set by the
user.

the Set statement is this:

_people[index] = value; or even _people.Insert(index,value);

but these throw 'ArgumentOutOfRange' exception stating: Must be
non-negative
and less than the size of the collection.

How can I use an array list & keep track of the index the user married
to
the value?

Thanks for any tips on this

Ant
Hi Ant,

As Truong Hong Thi said, the index is out of range. An ArrayList grows
dynamically as you put items in it, but you can only use indexes between0
and the number of already inserted items - 1. If you try to insert to an
index above the size of the ArrayList the index will be out of range, as
the exception tells you.

If you use a HashTable or Dictionary (.net 2.0) you can ignore the actual
position of the items and just store an item with its corresponding index
by simply adding a new item.

If you need the items to be ordered by indexes, and absolutely want to use
an ArrayList (or List in .Net 2.0), then you need to make sure the size of
the ArrayList is large enough (for instance by adding empty items until
the size is correct).

--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 25 '06 #4

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

Similar topics

11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
13
by: Kantha | last post by:
Hi all, I have declared an Union as follows typedef union { struct interrupt_bits { unsigned char c_int_hs_fs_status : 1, c_setup_intflag : 1,
14
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are...
19
Atli
by: Atli | last post by:
Introduction At some point, all web developers will need to collect data from their users. In a dynamic web page, everything revolves around the users input, so knowing how to ask for and collect...
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
9
by: Ajinkya | last post by:
Hello friends ! , I am very new to java script.If anyone can help me then I will be very very thankful to his/her. I am using php and mysql in my project and I have one textarea and one...
9
by: dgleeson3 | last post by:
Hello All I have a txt file of strings of different lengths. I dont know how many strings are in the file. I have no problem reading the file and sending to the console (as below). To...
4
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to 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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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.