C# 从 arrayList 中获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13072538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:32:52  来源:igfitidea点击:

getting a value out of an arrayList

c#arraylist

提问by Ryan Ward

I am using C# and I am creating an ArrayList called "importControlKeys" which creates fine. However, I can't for the life of my figure out a way to loop through the arrayList and pick out the values in the ArrayList for use in later code.

我正在使用 C#,我正在创建一个名为“importControlKeys”的 ArrayList,它可以很好地创建。但是,我终生无法找到一种方法来循环遍历 arrayList 并挑选出 ArrayList 中的值以供以后的代码使用。

I know I'm missing something easy, but what is the syntax to extract a value from an ArrayList. I was hoping it was somethign like importControlKeys[ii].value in my code below, but that isn't working.

我知道我错过了一些简单的东西,但是从 ArrayList 中提取值的语法是什么。我希望它在下面的代码中类似于 importControlKeys[ii].value ,但这不起作用。

I've searched on these boards and can't find the exact solution, though I'm sure it's easy. msot of the solutions say to re-write as a List but I have to believe there is a way to get data out of an array list without re-writing as a List

我已经在这些板上搜索过,但找不到确切的解决方案,尽管我确信这很容易。msot 的解决方案说要重写为列表,但我必须相信有一种方法可以从数组列表中获取数据,而无需重写为列表

private void button1_Click(object sender, EventArgs e)
        {
            ArrayList importKeyList = new ArrayList();
            List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
            foreach (DataGridViewRow row in grd1.Rows) 
            { 
                if (Convert.ToBoolean(row.Cells[Del2.Name].Value) == true)
                { 
                    rows_with_checked_column.Add(row);
                    importKeyList.Add(row.Cells[colImportControlKey.Name].Value);

                    //string importKey = grd1.Rows[grd1.SelectedCells[0].RowIndex].Cells[0].Value.ToString();
                    //grd1.ClearSelection();
                    //if (DeleteImport(importKey))
                    //    LoadGrid();
                }                
            }
            for (int ii = 0; ii < rows_with_checked_column.Count; ii++)
            {
                //grd1.ClearSelection();
                string importKey = importKeyList[ii].value;  //ERRORS OUT

                if (DeleteImport(importKey))
                    LoadGrid();

                // Do what you want with the check rows  
            }

        }

采纳答案by MethodMan

Not sure why you are using an ArrayList but if you need to loop thru it you could do something like this this

不知道为什么要使用 ArrayList 但如果您需要循环遍历它,您可以执行以下操作

if an element is not convertible to the type, you'll get an InvalidCastException. In your case, you cannot cast boxed int to string causing an exception to be thrown.

如果元素不可转换为该类型,您将收到 InvalidCastException。在您的情况下,您不能将 boxed int 转换为 string,从而导致抛出异常。

foreach (object obj in importKeyList ) 
{
    string s = (string)obj;
    // loop body
}

or you do a for loop

或者你做一个 for 循环

for (int intCounter = 0; intCounter < importKeyList.Count; intCounter++)
{
    object obj = importKeyList[intCounter];
    // Something...
}

回答by Sam I am says Reinstate Monica

well, what you can do instead is

好吧,你可以做的是

foreach(object o in importKeyList)
{ 
    string importKey = (string)o;
    // ...

}

you can replace the (string)with whatever type you need

你可以用(string)你需要的任何类型替换

回答by LightStriker

foreach (object o in importKeyList)
{ 
    // Something...
}

or

或者

for (int i = 0; i < importKeyList.Count; i++)
{
    object o = importKeyList[i];
    // Something...
}

回答by Francis P

You can't call .valueon your string list...

你不能调用.value你的字符串列表......

So this:

所以这:

string importKey = importKeyList[ii].value;

Should be:

应该:

string importKey = importKeyList[ii];

回答by Servy

You really shouldn't use an ArrayListin the first place, if you have a choice, you should use a List<T>. ArrayListhas been effectively deprecated since .NET 2.0. There are no advantages to using it over Listother than for legacy applications which can't or haven't been updated. The reason Listis preferred is that when you store data in an ArrayListit is just stored as an object, so an objectis what you get out. You need to cast it to what it really is so that you can use it.

你真的不应该ArrayList首先使用 an ,如果你有选择,你应该使用 a List<T>ArrayList自 .NET 2.0 起已被有效弃用。List除了用于无法或尚未更新的遗留应用程序之外,使用它没有任何优势。List首选的原因是,当您将数据存储在 an 中时,ArrayList它只是存储为 an object,因此 anobject是您得到的。您需要将其转换为实际情况,以便您可以使用它。

Without knowing the actual types that you're storing in the ArrayListI couldn't tell you what type of Listit should be, or what you need to cast the result into.

在不知道您存储在的实际类型的情况下,ArrayList我无法告诉您List它应该是什么类型,或者您需要将结果转换为什么类型。

Also note that you can use a foreachloop, rather than a forloop, to go through all of the items in the list/arraylist, which is often syntactically easier.

另请注意,您可以使用foreach循环而不是for循环来遍历列表/数组列表中的所有项目,这在语法上通常更容易。

回答by D Stanley

Just drop the .value:

只需删除.value

string importKey = (string)importKeyList[ii];

回答by Sreeju

for (int Counter = 0; Counter < Key.Count; Counter++)

{

object obj = Key[Counter];
}

for (int Counter = 0; Counter < Key.Count; Counter++)

{

object obj = Key[Counter];
}