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

foreach for ArrayList - unexpected behavior...

Hello all,
today i found a very strange situation of ArrayList - at least for me.

let's take 3 elementes that were put into list variable (list.add(point 1 -
3)):
Point(1,1);
Point(2,2);
Point(2,2);

list is an instance of ArrayList

foreach(Point p in list) {
Point p = (Point) list[list.indexOf(p)];
Console.Writeln( p.ToString());
}

in above example we will get only elements from 0 - 1
element (2) will be never printed cause list[list.indexOf(p)];
uses a hashcode (???) to find specified elements.

i have excpected searching no by hashcode but by reference (pointer to p).
it sounds very interesting... of course one can said that this is a lame or
invalid way of thinking...
but i had expected something else...

Adam
Nov 16 '05 #1
3 7034


"Kail" wrote:
Hello all,
today i found a very strange situation of ArrayList - at least for me.

let's take 3 elementes that were put into list variable (list.add(point 1 -
3)):
Point(1,1);
Point(2,2);
Point(2,2);

list is an instance of ArrayList

foreach(Point p in list) {
Point p = (Point) list[list.indexOf(p)];
Console.Writeln( p.ToString());
}


This code won't even compile. Aside from the misspelled method name (it's WriteLine) and the incorrect casing (it's IndexOf), you defined p twice.

After changing the loop to

Point point = (Point)list[list.IndexOf(p)];
Console.WriteLine(point.ToString());

the output is:

{X=1,Y=1}
{X=2,Y=2}
{X=2,Y=2}
Seeing that every value it accounted for, I guess either I don't understand your original problem or it was imagined in the first place.
Nov 16 '05 #2
Hi Adam,

May I ask why you do it this way? After all, the foreach loop will give you each Point in turn. No need to use the point to obtain it again. (Btw, the hashcode for all the points is 0)

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3
Kail wrote:
Hello all,
today i found a very strange situation of ArrayList - at least for me.

let's take 3 elementes that were put into list variable (list.add(point 1 -
3)):
Point(1,1);
Point(2,2);
Point(2,2);

list is an instance of ArrayList

foreach(Point p in list) {
Point p = (Point) list[list.indexOf(p)];
Console.Writeln( p.ToString());
}

in above example we will get only elements from 0 - 1
element (2) will be never printed cause list[list.indexOf(p)];
uses a hashcode (???) to find specified elements.

i have excpected searching no by hashcode but by reference (pointer to p).


ArrayList.IndexOf() calls Object.Equals() to perform its comparison,
hashcode is not used. Point is a value type, not a class, so bitwise
equality is used instead of reference equality. When p is a Point(2,2),
list.IndexOf( p) will always return 1 for the list you've given because
that's the first element that matches - it doesn't matter where that
instance of Point(2,2) came from.

Since Point is a value type, there is no concept of a reference for
instances of that type.

--
mikeb
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Lloyd Dupont | last post by:
Does any of you have the slightest ideas of why you can't modify an array list while in a foreach of its element. I wrote my own collection and I'm trying to add the same behavior but I don't...
4
by: Adam | last post by:
Hi all, The output of the code below is: This is Message 2 This is Message 2 But I expected: This is Message 1 This is Message 2
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
18
by: Ken Varn | last post by:
Is there any way to reset a foreach loop to re-iterate through the collection as if it were starting from the beginning? Namely, if I delete an item out of a collection, I want to be able to reset...
5
by: David C | last post by:
This is very strange. Say I have code like this. I am simply looping through a collection object in a foreach loop. Course course = new Course(); foreach(Student s in course.Students) {...
8
by: Maileen | last post by:
Hi, I would like to have something like an array (something like a collection) in order to store the following things : company name1, new1, old1, in1, out1 company name2, new2, old2, in2,...
10
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I wanted to go through each entry(?) of ArrayList and remove some particular entry. So I tried following but it throws exception at runtime: foreach (myEntry entry in myArrayList) {...
9
tlhintoq
by: tlhintoq | last post by:
Situation: A Windows Form with a single User Control containing 4 custom controls. This lets me drop the User Control on its own form to make it a dialog, or make it part of a larger configuration...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.