关于在C#中使用ArrayList的问题 [英] Problem About using ArrayList in C#

查看:112
本文介绍了关于在C#中使用ArrayList的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.

我有一个用于ex的结构:

  struct  MyObject
{
    点位置;
    颜色c;
} 



我正在使用ArrayList将它们保持在一起,例如:

 ArrayList MyList =  ArrayList();
MyObject obj =  MyObject();
obj.Location = 点( 0  0 );
obj.c = Color.White;
MyList.Add(obj); 



问题是

我想使用ArrayList中具有指定索引的对象的属性,例如:

 MyList [ 0 ].位置



但我必须先将MyList[0]转换为MyObject.因为这是Object类型,所以我无法访问MyObject的属性.

我该怎么办?


谢谢.

(顺便说一句)我不想使用普通数组,因为我将大量使用Add和RemoveAt函数.用于收藏的元素.

一般情况下:

 对象 whoKnowsWhat //  ... 
MyObject my = whoKnowsWhat  as  MyObject;
如果(我== nulll)//  ...做一些抛出异常或忽略的操作
我的./...使用我的

// 如果您已经确定是正确的课程
// (并且不为null,已经检查过),直接输入强制转换:
MyObject my =(MyObject)whoKnowsWhat;
// 如果错了,没问题,上面的代码将抛出异常,
// 这样您就可以恢复 



请勿根据您的情况进行所有操作!

只是不要使用名称空间System.Collections,它已经过时了;使用System.Collections.Generic代替.至少您将摆脱铸造问题.

—SA


请改用List<MyObject>. ArrayList不会让您做自己想做的事.


  public  结构 MyObj
{
    公共点位置;
     public 颜色c;
}
公共 无效 test()
{
    列表< MyObj> MyList =  List< MyObj>();
    MyObj [] abc =  MyObj [ 5 ];

     for ( int  i =  0 ; i <   5 ; i ++)
    {
        abc [i] .Location =  Point();
        abc [i] .c =  Color();

        // 要添加到列表中
        MyList.Add(abc [i]);
    }

    // 获取列表中的项目数
     int  count = MyList.Count;

    // 获取列表的容量
     int  cap = MyList.Capacity;

    // 从列表中删除所有元素
    MyList.Clear();

    // 将对象插入列表中指定索引的位置
    MyList.Insert( 2 ,abc [ 3 ]);

    // 从指定的索引中删除对象
    MyList.RemoveAt( 2 );

    // 从列表中删除所有元素
    MyList.Clear();
} 


Hello.

I have got a struct for ex:

struct MyObject
{
    Point Location; 
    Color c;
}



And I am using an ArrayList for keep them together ex:

ArrayList MyList = new ArrayList();
MyObject obj = new MyObject();
obj.Location = new Point(0, 0);
obj.c = Color.White;
MyList.Add(obj);



The problem is

I want to use an object''s properties from ArrayList with specified index like:

MyList[0].Location



but I have to convert MyList[0] to MyObject first. Because It is an Object type thats why I cannot reach MyObject''s properties.

How can I do that?


Thanks.

(Btw) I do not want to use normal arrays because I will be using Add and RemoveAt functions a lot.

解决方案

With classes like this you always will need type cast for collection''s element.

In general case:

object whoKnowsWhat //...
MyObject my = whoKnowsWhat as MyObject;
if (my == nulll) //... do something like throwing exception or ignoring
my./... use my

//If you're already sure is of the right class
//(and not null, already cheched), type cast directly:
MyObject my = (MyObject)whoKnowsWhat;
//if it's wrong, not problem, the code above will throw exception,
//so you could recover



Don''t do all that in your case!

Simply don''t use name space System.Collections, it''s mostly obsolete; use System.Collections.Generic instead. At least you will be away from the problems with casting.

—SA


Use a List<MyObject> instead. ArrayList is not going to let you do what you want to do.


public struct MyObj
{
    public Point Location;
    public Color c;
}
public void test()
{
    List<MyObj> MyList = new List<MyObj>();
    MyObj[] abc = new MyObj[5];

    for (int i = 0; i < 5; i++)
    {
        abc[i].Location = new Point();
        abc[i].c = new Color();

        //to add in the List
        MyList.Add(abc[i]);
    }

    //gets the number of items in the List
    int count = MyList.Count;

    //gets the capacity of List
    int cap = MyList.Capacity;

    //Removes All elements from the List
    MyList.Clear();

    //inser the object at the specified index in List
    MyList.Insert(2, abc[3]);

    //removes the object from specified index
    MyList.RemoveAt(2);

    //Removes all elements from the List
    MyList.Clear();
}


这篇关于关于在C#中使用ArrayList的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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