By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,563 Members | 1,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,563 IT Pros & Developers. It's quick & easy.

Accessing Arraylist Collection

P: n/a
JJ
Hi,

I have created an Arraylist object from an Arraylist class.
I added rows to the arraylist object and I need to find a particular record
in my arraylist. How do I do this? Also if I was in the middle of building
the arraylist and wanted to call the last record added to arraylist object
how can I do this?

Thanks,

JJ
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
JJ <JJ@discussions.microsoft.com> wrote:
I have created an Arraylist object from an Arraylist class.
I added rows to the arraylist object and I need to find a particular record
in my arraylist. How do I do this?
Do you mean by row number, or by row object? If it's by row object, use
IndexOf. If it's by row number, use the indexer:

object o = arrayList[rowNumber];
Also if I was in the middle of building the arraylist and wanted to
call the last record added to arraylist object how can I do this?


Use the indexer again, along with the Count property:

object last = arrayList[arrayList.Count-1];

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
JJ
ok so how would I update a record in an arrowlist? So how would I be able to
check or get individual items of arraylist?

Thanks,

JJ

"Jon Skeet [C# MVP]" wrote:
JJ <JJ@discussions.microsoft.com> wrote:
I have created an Arraylist object from an Arraylist class.
I added rows to the arraylist object and I need to find a particular record
in my arraylist. How do I do this?


Do you mean by row number, or by row object? If it's by row object, use
IndexOf. If it's by row number, use the indexer:

object o = arrayList[rowNumber];
Also if I was in the middle of building the arraylist and wanted to
call the last record added to arraylist object how can I do this?


Use the indexer again, along with the Count property:

object last = arrayList[arrayList.Count-1];

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3

P: n/a
JJ,

It works pretty much like an array of objects except that the number of
elements is dynamic rather than fixed. To update:

arrayList[rowNumber] = newThing;

To extract an item:

object oldThing = arrayList[rowNumber];

I'm leaving out casting; if the items in the arraylist are of a particular
type, say MyType:

MyType oldThing = (MyType)arrayList[rowNumber];

You should Google and read one of the basic tutorials and/or check out the
help for ArrayList -- particularly the Add(), AddRange(), IndexOf(),
Remove() and RemoveAt() methods, the Count property and of course the Item
property (indexer in C#) which I've focused on above.

--Bob

"JJ" <JJ@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
ok so how would I update a record in an arrowlist? So how would I be able
to
check or get individual items of arraylist?

Thanks,

JJ

"Jon Skeet [C# MVP]" wrote:
JJ <JJ@discussions.microsoft.com> wrote:
> I have created an Arraylist object from an Arraylist class.
> I added rows to the arraylist object and I need to find a particular
> record
> in my arraylist. How do I do this?


Do you mean by row number, or by row object? If it's by row object, use
IndexOf. If it's by row number, use the indexer:

object o = arrayList[rowNumber];
> Also if I was in the middle of building the arraylist and wanted to
> call the last record added to arraylist object how can I do this?


Use the indexer again, along with the Count property:

object last = arrayList[arrayList.Count-1];

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.