简体   繁体   English

从填充了另一个 ArrayList 的 ArrayList 中检索数据

[英]Retrieving datas from an ArrayList filled with another ArrayList

I got 2 ArrayList objects on my project defined as我的项目中有 2 个 ArrayList 对象定义为

ArrayList final_list = new ArrayList();
ArrayList temp_list = new ArrayList();

I needed to fill my final_list with the other one, so I used a loop using final_list.Add(temp_list) inside of it, which works fine.我需要用另一个填充我的final_list ,所以我在其中使用了一个使用final_list.Add(temp_list)的循环,效果很好。
Here is an exemple about what my final_list object looks like after the loop:这是一个关于我的final_list object 在循环后的样子的例子:

列表对象

The fact is I need to process the datas after the loop.事实是我需要在循环后处理数据。 So I guess I'll have to do something like所以我想我必须做类似的事情

for (int i = 0; i < final_list.Count; i++)
{
    for (int j = 0; j < 6 ; j++)
    {
        // Retrieving my [0,0] , [0,1], ... , [final_list.Count,5] datas
    }
}

but this isn't working.但这不起作用。
Anyway, I'm even struggling on the correct syntax to use so I already tried hard coded things like final_list[0,1] or final_list[0][1] to display the 10 value for exemple, but it doesn't work.无论如何,我什至在努力使用正确的语法,所以我已经尝试过硬编码,例如final_list[0,1]final_list[0][1]来显示10值,但它不起作用。
How can I manage to do that?我怎样才能做到这一点?

Seems you have a collection ( final_list ), which contains some collections inside.似乎您有一个集合( final_list ),其中包含一些 collections 内部。 And now you want to process the inner most elements.现在你想处理最里面的元素。
First of all, you'd better use generic collections instead of old ArrayList .首先,您最好使用通用的 collections 而不是旧的ArrayList
Generally, handling such a scenario can be done like this:通常,可以这样处理这种情况:

foreach( List<string> innerList in final_list)
{
     foreach( var innerMostItem in innerList)
     {
         // process the element!
     }
}

or equivelantely:或等价地:

foreach( var innerList in final_list)
{
     foreach( var innerMostItem in innerList as List<string>)
     {
         // process the element!
     }
}

By the way, naming convention of C#, is to use camelCase for variables.顺便说一下,C# 的命名约定是使用驼峰命名法来表示变量。

update更新
code snippet above is updated.上面的代码片段已更新。 Not the type casting in the inner loop !不是内循环中的类型转换
Though innerList was originally an IEnumerable , it is retrieved from the final_list as an instance of object class. You need to explicitly cast it to ArrayList or you will get an exception.虽然 innerList 最初是一个IEnumerable ,但它是作为object class 的实例从final_list中检索到的。您需要将其显式转换为ArrayList ,否则会出现异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM