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

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 2351
"Extremest" <dn**********@charter.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.Collections;

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**********@charter.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.com>
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.Insert() 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**********@charter.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
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...
11
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...
4
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...
2
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...
2
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...
4
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...
14
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...
3
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,...
2
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...
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: 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
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
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
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
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...

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.