473,569 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding new items to an arraylist

Is there a way to add new items to an arraylist witht he indexer? like
with a normal array I can just do like array[x] = new. What I have
read with arraylist it will throw an exception if I try to do this. Or
is there a way to find out how many things are actually in an array
without it counting the null sections? Say I have an array[10] but
only have 5 things in there how can I easily find out that there are 5?

Jun 13 '06 #1
5 2365
"Extremest" <dn**********@c harter.net> wrote:
Is there a way to add new items to an arraylist witht he indexer? like
with a normal array I can just do like array[x] = new. What I have
read with arraylist it will throw an exception if I try to do this.
Are you working with the ArrayList class? If so, the following code
works fine for me, with no exceptions thrown:

---8<---
using System;
using System.Collecti ons;

class App
{
static void Main()
{
ArrayList list = new ArrayList();
list.Add(null);
list.Add(null);

list[0] = new App();
list[1] = new App();
}
}
--->8---

Are you doing something different?
Or
is there a way to find out how many things are actually in an array
without it counting the null sections? Say I have an array[10] but
only have 5 things in there how can I easily find out that there are 5?


It isn't clear whether you're talking about an array or an ArrayList. To
count the number of non-null items in *either* an array *or* an
ArrayList, then you need to iterate through the list and compare with
null - i.e. do the counting yourself:

---8<---
int count = 0;
foreach (object o in a)
if (a != null)
++count;
--->8---

-- Barry

--
http://barrkel.blogspot.com/
Jun 13 '06 #2
I mean if oyu don't do the add part first and just want to add
something with a specific index number? I didn't know if there was a
way to see how many where in a normal array without doing the loop. I
don't want them to be counted if they are null though. That is what I
meant on the second part.

Jun 13 '06 #3
Extremest <dn**********@c harter.net> wrote:
I mean if oyu don't do the add part first and just want to add
something with a specific index number? I didn't know if there was a
way to see how many where in a normal array without doing the loop. I
don't want them to be counted if they are null though. That is what I
meant on the second part.


No, you can't add elements with the indexer.

--
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
Jun 13 '06 #4
Why not add the ArrayList element only when you add a non null object?
try to use:

ArrayList arr = new ArrayList();
while (condition)
arr.Add(new object());

or if you want to add an element to a specific location, you can use the
ArrayList.Inser t() methode.

Hope it's help you.
Mihaly

"Extremest" wrote:
Is there a way to add new items to an arraylist witht he indexer? like
with a normal array I can just do like array[x] = new. What I have
read with arraylist it will throw an exception if I try to do this. Or
is there a way to find out how many things are actually in an array
without it counting the null sections? Say I have an array[10] but
only have 5 things in there how can I easily find out that there are 5?

Jun 13 '06 #5
"Extremest" <dn**********@c harter.net> wrote:
I mean if oyu don't do the add part first and just want to add
something with a specific index number?


Perhaps you should look at the Hashtable class instead. You can add to
instances of that class using the indexer (which is actually the key of
the item to add).

-- Barry

--
http://barrkel.blogspot.com/
Jun 13 '06 #6

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

Similar topics

0
464
by: Stephen | last post by:
I have been getting on well with help from this forum trying to create an array list and work with it. Everything is working fine apart from displaying my array list items into the labels in my asp table. The asp table has numerous rows and each row has a label item within it. Im finding it impossible to get something to work which adds the...
11
2152
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...
4
5465
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new dropdownlist to the screen for selection. This continues until there are no children, and then it checks for a help article list based on that last...
2
2426
by: Bob Hollness | last post by:
Hi group. I am a newbie to ASP.NET as you will see from some of the questions I may ask! I have a datagrid which I have populated from a database. It works great! I have added a column, via the Columns dialog box from the properties of the datagrid, on the left that contains a select button. So now, when you press the button the whole...
2
2290
by: jack | last post by:
Hello, I need to dynamically add menu items to an existing menu on an MDI form. In the form load, when I create the menu items then add it to the menu control using the Add method, the entire menu dissappears. Any one know why? Below is the code I'm using to create the menu items and append them to the existing menu: Dim aDatasheets As...
4
1316
by: blisspikle | last post by:
The following sub form1_load reads in a text file that will say something like the text below. I think that the problem is that every time I add a class V_Sensor into the arraylist it puts it in as a reference. So in the end all of the arraylist items turn out to be the same as the last sensor in the text file. I am new and I do not know...
14
18673
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe is that as more and more are added, population of the list box takes longer and longer. ie the first 10th of the item set are added much much...
3
6964
by: Stuart | last post by:
I am using Visual Basic 2005. I have created a two dimensional ArrayList named aSystem that is populated as follows:- aSystem.Add(New PickList(0, "Undefined")) aSystem.Add(New PickList(-1, "Standard UTM")) aSystem.Add(New PickList(-2, "UTM-NAD83")) aSystem.Add(New PickList(-3, "UTM-NAD27"))
2
1491
by: wbosw | last post by:
I have a Arraylist (SearchSkillsArray) that contains objects. I want to place those objects into a listbox. Here i'm using get enumerator to loop through the arraylist and add the objects to the listbox. I'm receiving the error Conversion from type 'SkillSearch' to type 'String' is not valid. Does anyone have any suggestion to fix this error?...
0
7612
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...
0
7924
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. ...
0
6284
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...
1
5513
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...
0
5219
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...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
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...

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.