473,718 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArrayList problem.

GTi
If I use:

ArrayList TimeScale = new ArrayList();
TimeScale.Capac ity = 1000;
TimeScale[20]="test 1"

The last line trow me an error:
Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index

Is there anything I don't get here?

Jan 17 '06 #1
15 1704
ArrayList objects are not arrays - it is (essentially) a collection class;
initially (in general - some ctors allow preloading) they are empty. The
capacity is just the upper limit on what it can contain *without having to
grow*. The Count is the current amount of data. If you call .Add() 5 times,
then you will have 5 items, even if the capacity is 200, and [20] will fail.

Are you sure you mean to use ArrayList? You could create (for example) a
string array sized for 1000 items, and then [20] would work.

Marc
Jan 17 '06 #2
GTi <tu****@gmail.c om> wrote:
If I use:

ArrayList TimeScale = new ArrayList();
TimeScale.Capac ity = 1000;
TimeScale[20]="test 1"

The last line trow me an error:
Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index

Is there anything I don't get here?


Yes - the capacity isn't the same as the size. The capacity is how big
the ArrayList can become without having to "grow" by copying all its
data into a bigger backing array.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 17 '06 #3
An ArrayList is a Collection. Use the Add method to add an element to it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"GTi" <tu****@gmail.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
If I use:

ArrayList TimeScale = new ArrayList();
TimeScale.Capac ity = 1000;
TimeScale[20]="test 1"

The last line trow me an error:
Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index

Is there anything I don't get here?

Jan 17 '06 #4
GTi
NOTE:
This sample was just a example. In this samples I should use .add();
But the a value is any value between 0 and maxitems and not all values
in that range is used.

Jan 17 '06 #5
GTi
(where did the sample go?)

class TimeScale
{
public double TempSP = 0.0;
public double TempER = 0.0;
public double PresureSP = 0.0;
public double PresureER = 0.0;
}
maxitems=1000; // from a calculation
ArrayList TimeScale = new ArrayList(maxit ems);

for(int a=0; a<maxitems; a++)
{
TimeScale gts = new GraphTimeScale( );
gts.TempSP = somevalue2;
gts.TempER = somevalue3;
gts.PresureSP = somevalue4;
gts.PresureER = somevalue5;
TimeScale[a]=gts;

// NOTE in this sample I should use .add(gts);
but <a> can be any number from 0 to maxitems and not all indexes is
filled.

Jan 17 '06 #6
> GTi <tu****@gmail.c om> wrote:
If I use:

ArrayList TimeScale = new ArrayList();
TimeScale.Capac ity = 1000;
TimeScale[20]="test 1"
The last line trow me an error:
Index was out of range. Must be non-negative and less than the size
of
the collection.
Parameter name: index
Is there anything I don't get here?

Yes - the capacity isn't the same as the size. The capacity is how big
the ArrayList can become without having to "grow" by copying all its
data into a bigger backing array.


Most of us know that to be true. However, the docs would lead one to believe
it *should* work like the OP expected.

from ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 FEB.1033/cpref/html/frlrfSystemColl ectionsArrayLis tClassCapacityT opic.htm

"When the value of Capacity is set explicitly, the internal array is also
reallocated to accommodate the specified capacity."
Jan 17 '06 #7
GTi
One sample I have about 10000 items in a array.
The short discription is like this:

class TimeScale
{
public double TempSP = 0.0;
public double TempER = 0.0;
public double PresureSP = 0.0;
public double PresureER = 0.0;
public int StatusCode = 0;
}

maxitems=1000; // example
ArrayList TimeScale = new ArrayList(maxit ems);
for(int a=1; a<maxitems; a++)
{
GraphTimeScale gts = new GraphTimeScale( );
gts.TempSP = somevalue1;
gts.TempER = somevalue2;
gts.PresureSP = somevalue3;
gts.PresureER = somevalue4;
gts.StatusCode = somevalue5;
TimeScale[a] = gts;
}
Is there any other and better way of doing this?

Jan 18 '06 #8
chris martin <chris_m|NOSPAM |@caliber|SPAM| web.com> wrote:
Yes - the capacity isn't the same as the size. The capacity is how big
the ArrayList can become without having to "grow" by copying all its
data into a bigger backing array.


Most of us know that to be true. However, the docs would lead one to believe
it *should* work like the OP expected.

from ms-help:<snip>

"When the value of Capacity is set explicitly, the internal array is also
reallocated to accommodate the specified capacity."


That doesn't suggest that it should have worked. That talks about the
size of the internal array, *not* the logical size of the ArrayList
itself.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 18 '06 #9
GTi <tu****@gmail.c om> wrote:
One sample I have about 10000 items in a array.
The short discription is like this:

class TimeScale
{
public double TempSP = 0.0;
public double TempER = 0.0;
public double PresureSP = 0.0;
public double PresureER = 0.0;
public int StatusCode = 0;
}

maxitems=1000; // example
ArrayList TimeScale = new ArrayList(maxit ems);
for(int a=1; a<maxitems; a++)
{
GraphTimeScale gts = new GraphTimeScale( );
gts.TempSP = somevalue1;
gts.TempER = somevalue2;
gts.PresureSP = somevalue3;
gts.PresureER = somevalue4;
gts.StatusCode = somevalue5;
TimeScale[a] = gts;
}
Is there any other and better way of doing this?


Yes - call the Add method instead of using the indexer. Note that that
will fill the ArrayList from index 0 rather than index 1 (you know
you're only adding maxitems-1 items in the above, don't you?).

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

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

Similar topics

6
1832
by: Stephen | last post by:
Im trying to carry work out an else if clause in the below method but i'm having a lot of difficulty getting the correct code which allows me to check and see if an arraylist items has text in it containing Address not supplied. Im trying to do something below but its not working properly. Can someone help me do this please. the code i use is below. private void checkArrayList() { ArrayList alSearchaddress; alSearchaddress =...
4
1715
by: Hans De Schrijver | last post by:
I have a private ArrayList variable that holds objects of various types, though they're all derived from a common base class (User). What I would like to do is provide public accessor properties per type. I have written some code that does the trick now, but it involves looping through the private ArrayList and creating a new Array with just the objects of the type corresponding to the property. Problem is, this hapens every time you...
3
2855
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a datagrid. I am using the viewstate object to store the Arraylist items on the page on postback. My PROBLEM is that I need to redirect the user to a new aspx page and on this new page i need to be able to access the items in my arraylist. Is this...
9
5098
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test code below): (a)When adding BitArrays to the ArrayList and then looping through the ArrayList I seem to access only the latest added BitArray and I'm not exactly clear on best way to access each BItArray in the ArrayList (b)When I try to remove...
9
1413
by: Leon | last post by:
I have a webform in which when the user press generate button the form generate six unique ramdon numbers, the user can also type these six numbers in manually in any order. however, the user can also press a post button that post these six numbers into the database. My problem is that these six numbers need to be posted to the database from less to greatest, and all of my code is within the business tier class not the code behind class. ...
20
5975
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 arraylist then I have to handle this with a sort method that uses comparer. I can reference the properties of the Arraylist directly such as dim mylist as new FrameList mylist.Add(new FrameStructure) mylist(0).first = "blabla..." mylist(0).second...
0
1674
by: Grant Wickman | last post by:
Our team has just fixed a really nasty problem that appears to be caused by an obscure bug in the sort method of Arraylist and daisy-chained webservice stubs. We've fixed the bug so I'll not investigate any further but there was nothing about it when googling the newsgroups so I am posting a note in case it helps somebody else out. The problem was in a system with a web front end calling through to a chain of 3 webservices i.e.
18
3251
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point storage and dynamically resize it when there is a new point or should I use ArrayList? Is speed noticable between the two? Regards,
48
4474
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
8
4396
by: amazon | last post by:
I have a following structure that I am using with array list: Private Structure arrayliststruct Public Name As String Public value As String Public type As String End Structure and following function to format arraylist the way I want and calling it from button click event: Private Function formatarray(ByVal inarray As ArrayList, _
0
8723
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9354
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7987
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6652
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5971
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4741
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3182
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2122
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.