6 Replies - 2045 Views - Last Post: 21 October 2010 - 04:29 AM Rate Topic: -----

#1 svesce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-October 10

Accessing elements in an Object within an Arraylist

Posted 20 October 2010 - 04:21 PM

I have an ArrayList with one element comprised of an object with 4 elements:

? myArraylist[0]

{object[4]}
[0]: "item1"
[1]: "item2"
[2]: "item3"
[3]: "item4"

How do I access one of the elements?
Is This A Good Question/Topic? 0
  • +

Replies To: Accessing elements in an Object within an Arraylist

#2 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 181
  • View blog
  • Posts: 659
  • Joined: 03-February 10

Re: Accessing elements in an Object within an Arraylist

Posted 20 October 2010 - 04:29 PM


((object[])myArrayList[0])[x]



Where x is the index of the object array.
Was This Post Helpful? 1
  • +
  • -

#3 Guest_svesce*


Reputation:

Re: Accessing elements in an Object within an Arraylist

Posted 20 October 2010 - 04:32 PM

Awesome. Thank you very much!!
Was This Post Helpful? 0

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Accessing elements in an Object within an Arraylist

Posted 20 October 2010 - 04:33 PM

Cast the object from the ArrayList and then iterate through the elements using a loop.

Object[] myObject = (Object[])myArraylist[0];

for (int i = 0; i < myObject.Length; i++)
{
    //Do something here
    myObject[i];
}



Or if you know specifically which element of the object you want.
Object[] myObject = (Object[])myArraylist[0];

myObject[0];
myObject[1];
myObject[2];
myObject[3];


Was This Post Helpful? 1
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4574
  • View blog
  • Posts: 8,014
  • Joined: 08-June 10

Re: Accessing elements in an Object within an Arraylist

Posted 20 October 2010 - 05:52 PM

It's probably in your best interests to use a System.Collections.Generic.List instead of an ArrayList. ArrayList was a placeholder in .NET 1.0 until they implemented generics in 2.0.

List<object> myList = new List<object>();
myList.Add(new object());
object o1 = myList[0];


Except you can use any specific type of object.
Was This Post Helpful? 1
  • +
  • -

#6 Guest_svesce*


Reputation:

Re: Accessing elements in an Object within an Arraylist

Posted 20 October 2010 - 11:25 PM

Great point. Thanks for the heads up!
Was This Post Helpful? 0

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Accessing elements in an Object within an Arraylist

Posted 21 October 2010 - 04:29 AM

Please do away with the ArrayList and use a generic list. In .Net 1.1, the ArrayList was the only option for a collection other than an array. In .Net 2.0, Microsoft added generic lists(and other generic collections). If you are using .Net 2.0 or higher, then use the generic lists(like posted).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1