PC Review


Reply
Thread Tools Rate Thread

Adding new items to an arraylist

 
 
Extremest
Guest
Posts: n/a
 
      13th Jun 2006
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?

 
Reply With Quote
 
 
 
 
Barry Kelly
Guest
Posts: n/a
 
      13th Jun 2006
"Extremest" <(E-Mail Removed)> 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/
 
Reply With Quote
 
Extremest
Guest
Posts: n/a
 
      13th Jun 2006
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.

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      13th Jun 2006
Extremest <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?TWloYWx5?=
Guest
Posts: n/a
 
      13th Jun 2006
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?
>
>

 
Reply With Quote
 
Barry Kelly
Guest
Posts: n/a
 
      13th Jun 2006
"Extremest" <(E-Mail Removed)> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread-safety: Change property of items in arraylist versus removingitems from the arraylist Curious Microsoft Dot NET 2 6th Aug 2008 12:36 PM
ArrayList Items =?Utf-8?B?Um9iZXJ0?= Microsoft C# .NET 1 21st Jun 2007 12:41 AM
Re: adding items to an arrayList Darrel Microsoft ASP .NET 0 22nd Jun 2006 02:01 PM
Adding Arraylist Items to an asp table. Please help me =?Utf-8?B?U3RlcGhlbg==?= Microsoft Dot NET 0 22nd Oct 2004 03:47 PM
Adding an ArrayList to an ArrayList? Gustaf Liljegren Microsoft C# .NET 4 4th Nov 2003 12:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:03 AM.