Get an Object according to its index in ArrayList

F

Fir5tSight

Hi,

I declare PageObjectsArray1 and PageObjectsArray2 as ArrayList. Each
element in the array is a Page object. Now I want to compare every pair
of Page objects of the same index in the two arrays.

However, it seems that there is no method in ArrayList such as
"GetAt(i)" so that I can return a Page object at a particular location
in the array. Could anyone help me to find out there is such method or
an alternative?

FYI, I have:

foreach (Page p1 in PageObjectsArray1)
{
//I want to get a Page object with the same index for
PageObjectsArray2 so that I can compare the two pages.
}

Many thanks!
 
R

R. MacDonald

Hello, Fir5tSight,

Perhaps you are looking for the "IndexOf" method. E.g.

PageObjectsArray2.Item(PageObjectsArray1.IndexOf(p1))

But wouldn't it be easier to just iterate through the indices of the
ArrayList?

Eg.

For ItemNumber As Integer = 0 To PageObjectsArray1.Count - 1
' Comparisons, etc.
Next ItemNumber

If you do elect to use the IndexOf method, note that it returns the
index of the first matching entry in the array list. For your stated
purpose this probably doesn't matter, but for some applications it
could. You can use other overloads of IndexOf to get around this if it
is a problem.

Cheers,
Randy
 
T

Tasos Vogiatzoglou

for (int i=0;i<PageObjectsArray1.Legnth;i++){
if (i<PageObjectsArray2.Length) {
comparison =
PageObjectsArray1.Equals(PageObjectsArray2);
}
}
 
F

Fir5tSight

Hi Tasos,

Many thanks for the suggestion! This is exactly what I needed. Thanks
again.
 
F

Fir5tSight

Hi Randy,

The issue was that I needed to get the element in a certain position in
an ArrayList. Tasos gave me the answer I was looking for. Thanks for
the help though!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top