数组中的对象...... [英] objects in arrays...

查看:43
本文介绍了数组中的对象......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我定义的一些对象:


class myItem

{

public string szItemName ="" ;

public string szItemValue ="" ;;

public string szOrderNumber =""

}

收集到ArrayList中


ArrayList aryItems = new ArrayList();

//这里填充aryItems的代码


现在说我用一个foreach来完成数组。循环,并更改一些

值,我是否需要执行以下操作?


foreach(myItem itm in aryItems)

{


if(itm.szItemName ==" fred")

{

//首先删除元素数组

int nIndex = aryItems.IndexOf(itm);

aryItems.Remove(itm);


//现在更改以某种方式对象

itm.szItemValue =" flintstone";


//将对象重新插入数组

aryItems.InsertAt(itm)

}


}


或者我可以这样做吗?


foreach(myItem itm in aryItems)

{


if(itm.szItemName ==" fred")

{

//只是以某种方式更改对象,这个

itm.szItemValue =" flintstone";

}


}

基本上是foreach枚举是每个对象被复制到我引用的另一个

对象,或者它是否提供了一个返回对象的点

集合中存在...

解决方案

<" Dan" < danielbass [at] postmaster [dot] co [dot] uk>>写道:

给定一些我定义的对象:

class myItem
{
public string szItemName ="" ;;
公共字符串szItemValue ="" ;;
公共字符串szOrderNumber =""

收集到ArrayList中

ArrayList aryItems = new ArrayList();
//在这里填充aryItems的代码

现在说我用一个foreach来完成数组。循环,并更改一些值,我是否需要执行以下操作?

foreach(myItem itm in aryItems)
{

if(itm) .szItemName ==" fred")
//首先从数组中删除元素
int nIndex = aryItems.IndexOf(itm);
aryItems.Remove(itm );

//现在以某种方式更改对象
itm.szItemValue =" flintstone";

//将对象重新插入数组
aryItems.InsertAt(itm)
}

}


你不能这样做,一开始 - 你可以'当你迭代它时,不要修改集合

。你会在运行时得到一个例外。

或者我可以这样做吗?

foreach(myItem itm in aryItems)


/> if(itm.szItemName ==" fred")
//只是以某种方式更改对象,这个// //更新存储在集合中的对象...
itm.szItemValue =" flintstone";
}
}


是的,你可以这样做。

基本上在foreach枚举中,每个对象都被复制到我引用的另一个对象,或者它是否提供了一个指向集合中存在的对象的点......




该对象根本不在集合中。这个系列中的内容是

a *参考*,它就是你给出的参考资料。


我建议你阅读参考类型。我关于参数

传递的文章有关于此的一些信息 - 我还没有完成关于这个主题的写一篇

的完整文章。
http://www.pobox.com/~skeet/csharp/parameters。 HTML


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复对于小组,请不要给我发邮件


谢谢Jon。


如果使用索引计数器的实例怎么样? ?

而不是

foreach(...)

它是

for(x = 0; x < aryItems.Count; x ++)...


同样的事情?


谢谢。


Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...

<" Dan" < danielbass [at] postmaster [dot] co [dot] uk>>写道:

给定一些我定义的对象:

class myItem
{
public string szItemName ="" ;;
公共字符串szItemValue ="" ;;
公共字符串szOrderNumber =""

收集到ArrayList中

ArrayList aryItems = new ArrayList();
//在这里填充aryItems的代码

现在说我用一个foreach来完成数组。循环,并更改一些值,我是否需要执行以下操作?

foreach(myItem itm in aryItems)
{

if(itm) .szItemName ==" fred")
//首先从数组中删除元素
int nIndex = aryItems.IndexOf(itm);
aryItems.Remove(itm );

//现在以某种方式更改对象
itm.szItemValue =" flintstone";

//将对象重新插入数组
aryItems.InsertAt(itm)


}



你不能这样做,一开始 - 你可以'在你迭代它的同时修改集合。你会在运行时遇到异常。

或者我可以这样做吗?

foreach(myItem itm in aryItems)
{

if(itm.szItemName ==" fred")
//只是以某种方式改变对象,这个//更新存储的对象该集合也是......
itm.szItemValue =" flintstone";
}
}



是的,你可以做到。

基本上在foreach枚举中是每个对象被复制到我引用的另一个对象,或者它是否提供了一个指向对象的点
收藏中存在...



对象根本不在收藏中。该系列中的内容是* *参考*,它是您给出的参考资料。

我建议您阅读参考类型。我关于参数传递的文章有一些相关的信息 - 我还没有完成关于这个主题的完整文章。
http://www.pobox.com/~skeet/csharp/parameters.html
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



<" Dan = o\)" < danielbass [at] postmaster [dot] co [dot] uk>>写道:

使用索引计数器的实例怎么样?
而不是
foreach(...)
它是(x) = 0; x< aryItems.Count; x ++)...

同样的事情?




这是同样的事情集合中的内容,但是你可以修改集合。当然,如果你删除当前, item,

所有东西都转移到一个地方,所以如果你不小心你

最终会错过下一个项目。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件


Given some object that I define as:

class myItem
{
public string szItemName = "";
public string szItemValue = "";
public string szOrderNumber = ""
}

that is collected into an ArrayList

ArrayList aryItems = new ArrayList();
// code to populate aryItems here

Now say I go through the array with a "foreach" loop, and change some
values, do I need to do the following?

foreach ( myItem itm in aryItems )
{

if ( itm.szItemName == "fred" )
{
// first remove the element from the array
int nIndex = aryItems.IndexOf ( itm );
aryItems.Remove (itm);

// now change the object in some way
itm.szItemValue = "flintstone";

// reinsert the object back into the array
aryItems.InsertAt ( itm )
}

}

or can I do this?

foreach ( myItem itm in aryItems )
{

if ( itm.szItemName == "fred" )
{
// just change the object in some way, this
// updates the object in stored in the collection as well...
itm.szItemValue = "flintstone";
}

}
Basically in the foreach enumeration is each object being copied to another
object that I reference, or does it provide a point back to the object
existing in the collection...

解决方案

<"Dan" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:

Given some object that I define as:

class myItem
{
public string szItemName = "";
public string szItemValue = "";
public string szOrderNumber = ""
}

that is collected into an ArrayList

ArrayList aryItems = new ArrayList();
// code to populate aryItems here

Now say I go through the array with a "foreach" loop, and change some
values, do I need to do the following?

foreach ( myItem itm in aryItems )
{

if ( itm.szItemName == "fred" )
{
// first remove the element from the array
int nIndex = aryItems.IndexOf ( itm );
aryItems.Remove (itm);

// now change the object in some way
itm.szItemValue = "flintstone";

// reinsert the object back into the array
aryItems.InsertAt ( itm )
}

}
You can''t do that, to start with - you can''t modify the collection
while you''re iterating through it. You''ll get an exception at runtime.
or can I do this?

foreach ( myItem itm in aryItems )
{

if ( itm.szItemName == "fred" )
{
// just change the object in some way, this
// updates the object in stored in the collection as well...
itm.szItemValue = "flintstone";
}
}
Yup, you can do that.
Basically in the foreach enumeration is each object being copied to another
object that I reference, or does it provide a point back to the object
existing in the collection...



The object isn''t in the collection at all. What''s in the collection is
a *reference*, and it''s that reference which you''ve been given.

I suggest you read up on reference types. My article on parameter
passing has some information on this - I haven''t got round to writing a
full article on the topic yet.
http://www.pobox.com/~skeet/csharp/parameters.html

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


Thanks Jon.

What about an instance where an index counter is used?
Instead of
foreach (...)
it was
for ( x = 0; x < aryItems.Count; x++ )...

Same thing?

Thanks.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

<"Dan" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:

Given some object that I define as:

class myItem
{
public string szItemName = "";
public string szItemValue = "";
public string szOrderNumber = ""
}

that is collected into an ArrayList

ArrayList aryItems = new ArrayList();
// code to populate aryItems here

Now say I go through the array with a "foreach" loop, and change some
values, do I need to do the following?

foreach ( myItem itm in aryItems )
{

if ( itm.szItemName == "fred" )
{
// first remove the element from the array
int nIndex = aryItems.IndexOf ( itm );
aryItems.Remove (itm);

// now change the object in some way
itm.szItemValue = "flintstone";

// reinsert the object back into the array
aryItems.InsertAt ( itm )
}

}



You can''t do that, to start with - you can''t modify the collection
while you''re iterating through it. You''ll get an exception at runtime.

or can I do this?

foreach ( myItem itm in aryItems )
{

if ( itm.szItemName == "fred" )
{
// just change the object in some way, this
// updates the object in stored in the collection as well...
itm.szItemValue = "flintstone";
}
}



Yup, you can do that.

Basically in the foreach enumeration is each object being copied to
another
object that I reference, or does it provide a point back to the object
existing in the collection...



The object isn''t in the collection at all. What''s in the collection is
a *reference*, and it''s that reference which you''ve been given.

I suggest you read up on reference types. My article on parameter
passing has some information on this - I haven''t got round to writing a
full article on the topic yet.
http://www.pobox.com/~skeet/csharp/parameters.html

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



<"Dan =o\)" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:

What about an instance where an index counter is used?
Instead of
foreach (...)
it was
for ( x = 0; x < aryItems.Count; x++ )...

Same thing?



It''s the same thing in terms of what''s in the collection, but there you
can modify the collection. Of course, if you remove the "current" item,
everything gets shifted down one place, so if you''re not careful you
can end up missing the next item.

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


这篇关于数组中的对象......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆