Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
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.
Posted
Updated 19-Nov-10 20:01pm
v3
Comments
Simon Dufour 30-Aug-10 8:56am    
Reason for my vote of 2
Question wasn't clear. In the question, he talks of ArrayList as a constraint but accept an answer that do not use them.

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


VB
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.
VB
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.
VB
orow(0).Item("gratde").ToString()
 
Share this answer
 
v3
Comments
Ramya 2010 26-Aug-10 2:01am    
My ArrayList is a Collection of student Objects.These Objects contain a datamember called department.If i want to display science department Student details means, i have to take those student info from the array list without using 'For' loop and linq and stores those info in a list
Ramya 2010 26-Aug-10 2:31am    
wil 'Datatableavg.Select("grade='A'")' return a list of Student info(whose grade is equal to 'A')?
There's the recursive approach with . . .

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

A generic recursive sort function:
C#
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:
C#
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);
}



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.
 
Share this answer
 
Comments
Simon Dufour 26-Aug-10 10:17am    
Reason for my vote of 5
Ugly but that's what he asked for.
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.
 
Share this answer
 
Comments
Simon_Whale 26-Aug-10 10:56am    
Reason for my vote of 3
My reason for a 3 the orignal poster said that he doesn't want to use a FOR loop, so the solution even though correct wasnt what the OP was after. Best would be to use lists
Simon Dufour 26-Aug-10 13:19pm    
Yes, I know. I suggested the first answer in case the original poster didn't want to use a FOR loop because he thought he'd have to loop through the whole list.
You could try the BinarySearch[^] method.
 
Share this answer
 
Comments
Simon Dufour 26-Aug-10 8:11am    
First, a binary search assume that the ArrayList is sorted. Second, a binary search will only return ONE element.. not a whole list.
Simon Dufour 26-Aug-10 8:12am    
Reason for my vote of 2
Is not a solution.
Change to a List (as opposed to an ArrayList), and either write the appropriate sort delegates, or use the code from this tip/trick:

A Generic Comparison Class for Collection Items[^]

BTW, this smells like a homework assignment. Only college instructors would impose moronic don't-use-these-features requirements on a project.

I mean, seriously...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900