从ArrayList收集项目列表? [英] Collecting a list of items from an ArrayList?

查看:82
本文介绍了从ArrayList收集项目列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的申请书有问题.我将学生信息对象的列表存储在ArrayList中.现在,我需要从成绩为"A"的ArrayList中提取学生名单.如何在不使用For 循环或Linq的情况下从ArrayList 提取此学生信息对象列表?

在此先感谢您.

Hi,

I have a problem in my application. I''m storing a list of student information objects in an ArrayList. Now I''m in a situation where I need to extract the list of students from an ArrayList, whose grade is equal to "A". How can I extract this list of student information objects from an ArrayList without using a For loop or Linq?

Thanks in advance.

推荐答案

您可以使用数据表代替数组列表.您可以选择对数据表进行排序.


Instead of Array List you can use the data table. you r having a option to sort in data table.


datatableavg = CreateDatatable() 'you have to create a data table here with the student details and grade one column. in your function i have not specified how to create a data table.   
       'then u have to sort by the bellow statement.
       datatableavg .DefaultView.Sort = "grade"
       datatableavg = CType(datatableavg .DefaultView.ToTable(), DataTable)


那么数据表将包含排序后的值.您可以使用它.


then the datatable will contain the sorted value. you can use it.

Dim orow() As DataRow = datatableavg .Select("grade='A'")
'the orow() is the row collection it contain the required filtered
 'value.


然后要访问第一行,您必须使用以下语句.


then to access the first row you have to use the following statement.

orow(0).Item("gratde").ToString()


使用的递归方法. . .

源对象:
There''s the recursive approach with . . .

A source object:
class MyObj
{
    public int AnInteger;
}


通用递归排序函数:


A generic recursive sort function:

private static void GetValues<T>(List<T> sourceList, int iter, List<T> resultList, Func<T, bool> compareFunc)
{
    if (iter >= sourceList.Count)
    {
        return;
    }
    if (compareFunc(sourceList[iter]))
    {
        resultList.Add(sourceList[iter]);
    }
    GetValues(sourceList, iter + 1, resultList, compareFunc);
}


和一个使用案例:


and a usage case:

List<MyObj> myList = new List<MyObj>();
for (int i = 0; i < 10; i++)
{
    myList.Add(new MyObj() { AnInteger = i });
}
List<MyObj> results = new List<MyObj>();
GetValues(myList, 0, results, obj => obj.AnInteger < 5);
foreach (MyObj obj in results)
{
    Trace.WriteLine("Obj: " + obj.AnInteger);
}




希望这是一项家庭作业或某种练习,因为在这种情况下将自己限制为无循环且无限制是很愚蠢的.到目前为止,此递归示例并非最佳解决方案性能.另外,现在ListArrayList更为可取.




Hopefully this is a homework assignment or some kind of exercise because limiting yourself to no looping and no linq in this case is kind of silly. This recursive example is by far not the best solution performance wise. Also, Lists are preferred over ArrayLists now.


如果您想要一种简单的方法,可以保留已知的ArrayList 按等级排序的内容.然后,您只需要从开始到要搜索的年级末进行循环,而不是整个列表.该算法仍然是 O(n),因为您必须在每个项目上循环.

另一个解决方案是为每个年级保持ArrayList .这样,可以在 O(1)中找到所有年级的学生,但是无论如何在屏幕上显示它们都是 O(n).

除了将内容存储在ArrayList中之外,您还可以将它们放置在数据库中,例如Access数据库.然后,您可以使用一些简单的SQL快速获取所需的任何信息.这绝对是最好的解决方案. 从未创建ArrayList来快速过滤或排序数据.
If you want an easy way, you could keep an ArrayList that you know is sorted by grade. Then, you''d only have to make a loop from start up to the end of the grade you''re searching for instead of the whole list. The algorithm would still be O(n) since you''d have to loop on every item.

Another solution is to keep an ArrayList for every grade. This way, finding all the student of a grade could be done in O(1) but showing them on the screen is O(n) anyway.

Instead of storing stuff in an ArrayList, you could put them in a Database.. like an Access Database. Then, you could use some simple SQL to rapidly get any information you want. This is definitely the best solution. ArrayList were never created to rapidly filter or sort data.


这篇关于从ArrayList收集项目列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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