问题描述
我的申请书有问题.我将学生信息对象的列表存储在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.
推荐答案
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()
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);
}
希望这是一项家庭作业或某种练习,因为在这种情况下将自己限制为无循环且无限制是很愚蠢的.到目前为止,此递归示例并非最佳解决方案性能.另外,现在List
比ArrayList
更为可取.
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, List
s are preferred over ArrayList
s now.
这篇关于从ArrayList收集项目列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!