如何访问ArrayList成员中的数组元素? [英] How to access an array element in a member of ArrayList?

查看:88
本文介绍了如何访问ArrayList成员中的数组元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个ArrayList,其成员是类。这些类派生自相同的基类。基类有一个浮点数组和一个字符串。如何访问ArrayList成员中浮点数组中的每个元素?

任何帮助将不胜感激。


Haobin

Hi everyone,
I have an ArrayList whose members are classes. These classes are derived from a same base class. The base class has a floating point array and a string. How do I access each element in the floating point array in a member of the ArrayList?
Any help would be greatly appreciated.

Haobin

推荐答案

Haobin< an ******* @ discussion.microsoft.com>写道:
Haobin <an*******@discussions.microsoft.com> wrote:
我有一个ArrayList,其成员是类。这些类来自同一个基类。基类有一个浮点数组和一个字符串。如何访问ArrayList成员中浮动
点数组中的每个元素?
我们非常感谢任何帮助。
I have an ArrayList whose members are classes. These classes are
derived from a same base class. The base class has a floating point
array and a string. How do I access each element in the floating
point array in a member of the ArrayList?
Any help would be greatly appreciated.




As总是在编程中,诀窍就是解决问题:


1)如何将数组列表中的元素作为基类?

2)如何在我的基础实例中访问浮点数组

类?

3)如何访问浮点数组中的每个元素?

1)很容易 - 只是演员:


BaseClass foo =(BaseClass)myArrayList [index];


2 )取决于你的基类


3)很简单 - 只需索引:


float f = myFloatArray [index];


-

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

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



As always in programming, the trick is to break the problem down:

1) How do I get an element in my array list as the base class?
2) How do I access the floating point array in an instance of my base
class?
3) How do I access each element in a floating point array?
1) is easy - just cast:

BaseClass foo = (BaseClass) myArrayList[index];

2) depends on your base class

3) is easy - just index:

float f = myFloatArray[index];

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


H. aobin,


我认为这是你正在谈论的场景......

--------------- -------------------------------------------------- -----------

--------------------

class SomeObject

{

public float fMyNumber; //或者它是一个浮点数组?无所谓

无论如何。

公共字符串szMyString;

...

}


ArrayList aryOfObjects ... //包含或将包含SomeObjects的加载

----------------- -------------------------------------------------- ---------

--------------------

void SomeMethodSomewhere()<对于阵列中的所有对象,
{


//

//

//


SomeObject so;

for(int a = 0; a< aryOfObjects.Count; a ++)

{

if(aryOfObjects [a]是SomeObject)

{

so =(SomeObject)aryOfObjects [a];

listbox1。 Items.Add(so.fMyNumber.ToString()); //添加浮点数

一些预定列表

listbox2.Items.Add(so.szMyString.ToString()); //添加字符串

到某些预定列表

}

else

{

//报告错误?

继续;

}


}


}

---------------------------------------- ------------------------------------

------ --------------


这有意义吗?当然,将你的

成员变量保密,并通过属性访问它们是更好的代码练习。

如果你有更多的probs,请告诉我。 />

Dan。


" Haobin" <一个******* @ discussions.microsoft.com>在消息中写道

news:04 ********************************** @ microsof t.com ...
Haobin,

I assume this is the scenario you are talking about...
----------------------------------------------------------------------------
--------------------
class SomeObject
{
public float fMyNumber; // or is it an array of floats? doesn''t matter
either way.
public string szMyString;
...
}

ArrayList aryOfObjects... // contains, or will contain loads of SomeObjects
----------------------------------------------------------------------------
--------------------
void SomeMethodSomewhere()
{

//
// for all objects in the array
//

SomeObject so;
for ( int a = 0; a < aryOfObjects.Count; a++ )
{
if ( aryOfObjects[a] is SomeObject )
{
so = (SomeObject)aryOfObjects[a];
listbox1.Items.Add ( so.fMyNumber.ToString() ); // add float to
some predifined list
listbox2.Items.Add ( so.szMyString.ToString() ); // add string
to some predifined list
}
else
{
// report some error?
continue;
}

}

}
----------------------------------------------------------------------------
--------------------

Does that make sense? Of course it''s better code practice to keep your
member variables private, and access them through properties.
Let me know if you have any more probs.

Dan.

"Haobin" <an*******@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
大家好,
我有一个ArrayList,其成员是类。这些类是从同一个基类派生的
。基类有一个浮点数组和一个

字符串。如何在ArrayList的成员

中访问浮点数组中的每个元素?任何帮助将不胜感激。

Haobin
Hi everyone,
I have an ArrayList whose members are classes. These classes are derived from a same base class. The base class has a floating point array and a
string. How do I access each element in the floating point array in a member
of the ArrayList? Any help would be greatly appreciated.

Haobin



下面是一个小的转折,可以稍微提高效率我猜你是

同时检查类型和演员。


SomeObject so = null;

for(int a = 0; a < aryOfObjects.Count; a ++)

{

so = aryOfObjects [a] as SomeObject;

if(so == null)

抛出新的MyException(" foo");

listbox1.Items.Add(so.fMyNumber.ToString());

.. ..

}


-

William Stacey,MVP
Below is a small twist that can be slightly more efficient I guess as you
check the type and cast at same time.

SomeObject so = null;
for ( int a = 0; a < aryOfObjects.Count; a++ )
{
so = aryOfObjects[a] as SomeObject;
if ( so == null )
throw new MyException("foo");
listbox1.Items.Add(so.fMyNumber.ToString());
....
}

--
William Stacey, MVP
void SomeMethodSomewhere()
//
//对于数组中的所有对象
//

SomeObject so;
for(int a = 0; a< aryOfObjects.Count; a ++)
{
if(aryOfObjects [a] is SomeObject)
{
so =(SomeObject)aryOfObjects [a];
李stbox1.Items.Add(so.fMyNumber.ToString()); //将float
添加到某个预定列表中
listbox2.Items.Add(so.szMyString.ToString()); //将字符串添加到某些预定列表中
}

//
//报告错误?
继续;
}

}

}
------------------------------ --------------------------------------------
- --------------------

这有意义吗?当然,将你的
成员变量保密,并通过属性访问它们是更好的代码练习。
如果你有更多的probs,请告诉我。

Dan。

Haobin <一个******* @ discussions.microsoft.com>在消息中写道
新闻:04 ********************************** @ microsof t.com。 ..
void SomeMethodSomewhere()
{

//
// for all objects in the array
//

SomeObject so;
for ( int a = 0; a < aryOfObjects.Count; a++ )
{
if ( aryOfObjects[a] is SomeObject )
{
so = (SomeObject)aryOfObjects[a];
listbox1.Items.Add ( so.fMyNumber.ToString() ); // add float to some predifined list
listbox2.Items.Add ( so.szMyString.ToString() ); // add string
to some predifined list
}
else
{
// report some error?
continue;
}

}

}
-------------------------------------------------------------------------- -- --------------------

Does that make sense? Of course it''s better code practice to keep your
member variables private, and access them through properties.
Let me know if you have any more probs.

Dan.

"Haobin" <an*******@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
大家好,
我有一个ArrayList,其成员是类。这些类派生自相同的基类。基类有一个浮点数组和一个
字符串。如何在ArrayList的
Hi everyone,
I have an ArrayList whose members are classes. These classes are derived from a same base class. The base class has a floating point array and a
string. How do I access each element in the floating point array in a



成员中访问浮点数组中的每个元素?


member of the ArrayList?

任何帮助将不胜感激。

Haobin
Any help would be greatly appreciated.

Haobin







这篇关于如何访问ArrayList成员中的数组元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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