Join 132,180 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,446 people online right now. Registration is fast and FREE... Join Now!
I'm trying to search an array list, but i do not succed. It is sorting correct, but do not found anything i search for. tried with an IndexOf and a BinarySearch but dosen't seems to work.
Well remember that your arraylist is going to be full of Student objects. So in order to do a sort for instance you are going to have to tell C# how to compare your student objects. So hopefully you have done that. If not, look into implementing the IComparable interface on your Student class and then you will need to define the compareTo() method.
You can see an example of this at the following page...
Anything that sorts will need such an interface if it is a customized object you are storing so that it knows how to compare each for sorting.
You also have to understand that since your arraylist also stores objects that the item you pass to BinarySearch is also an object. The BinarySearch method which takes one parameter is going to also use the default comparer to see if the two objects are equal. Now BinarySearch also takes other parameters optionally which allows you to specializing a comparing function which you can then use to do the comparison.
But for simplicity sake, either create a student object that is identical to the one stored in the ArrayList or do a search algorithm which simply looks at objects and asks the object to return values. Below is an example of how this is done using a sequential search....
// List Student items foreach (Student item in myArrayList) { // We implemented a ToString function and we compare the // search terms to the item by asking the item to return its // value. If they match, we found the item. if (searchCource == item.ToString()) { Console.WriteLine("Found at element: " + index); } index++; }
Again this is a sequential search (for simplicity) to show you how each item in the arraylist is an object so we have to ask it to reveal its information. Otherwise if we use the binarysearch we can pass it a student object to look for (not a string in this case). IndexOf works similar. You pass it an object to look for. Now if that object is a string, fine, pass it a string but in this case you would have to pass it a Student object to look for in the collection.
Hope I am making sense to you.
This post has been edited by Martyr2: 5 Oct, 2008 - 11:01 AM
Hi, Thanks for the answer. I'm doing an sort with IComarable, but since this is my first ArrayList i'm not quit getting it how it works. If i under stand correctly the myArrayList looks like this
[0] - Student [1] - Student [2] - Student
and every Student object contains name, birth, cource, grade. I was trying do like this : if (myArrayList [index].name eq searchCource) { print(student); }
but that diden't work i also tried :
Console.WriteLine(myArrayList.IndexOf("Mikael")); returned three -1 what i belive is that it wasen't found, but tried to print out the one it found but wasen't enable to.
adding the hole code : program.cs
csharp
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections;
namespace HomeWork3_6 { class Program { static void Main(string[] args) {
Console.WriteLine("\nArrayList - Added new entry");
foreach (Student i in myArrayList) { Console.WriteLine(" " + i); } break;
case 2: Console.Write("Search CourceName : <cource>\n"); string searchCource = Console.ReadLine();
// Sort items myArrayList.Sort();
int index = 0;
// List Student items foreach (Student item in myArrayList) { Console.WriteLine(myArrayList.IndexOf("Mikael"));
// We implemented a ToString function and we compare the // search terms to the item by asking the item to return its // value. If they match, we found the item. if (searchCource == item.ToString()) { Console.WriteLine("Found at element: " + index); } index++; }
// Perform a search //Console.WriteLine("'Mikael' found at index {0}", myArrayList.BinarySearch("Mikael"));
break;
case 3:
Console.WriteLine("\nArrayList - Before sorted ");
foreach (Student i in myArrayList) { Console.WriteLine(" " + i); }
//sort the array list myArrayList.Sort();
Console.WriteLine("\nArrayList - Sorted by Name");
foreach (Student i in myArrayList) { Console.WriteLine(" " + i); }
but sorry now am i lost, i thought the CompareTo was only for sorting?
is List<Student> easier? never heard of!
but the problems seems to be that it dosen't search inside the Object, since if i only add Console.WriteLine(myArrayList.IndexOf("Mikael")); does that not only search for an Object named Mikael and not inside it?
Sorry for the confusion. Yes, CompareTo is for sorting. The IndexOf uses the Equals method. It's just that if you implement CompareTo you pretty much get Equals for free:
However, you're not just asking for it to compare to name, but to a partial name. You essentially want to do a search. You'll have write your own for loop for that. Or use a List<>...
Here's some code. Hope it makes sense and gives you some ideas.
csharp
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Diagnostics;
// expose name to public public string Name { get { return this.name; } }
public int CompareTo(object obj) { if (obj==null) { return -1; } if (this.GetType()==obj.GetType()) { Student e = (Student)obj; return this.name.CompareTo(e.name); // ascending } return this.name.CompareTo(obj.ToString()); // ascending }
// overide Equals, to lookup by name. public override bool Equals(object obj) { return this.CompareTo(obj) == 0; }
// if you override Equals, you must override hash code // just give it the hashCode of name and it's fine public override int GetHashCode() { return this.name.GetHashCode(); }
class Program { protected ArrayList myArrayList = new ArrayList(); protected List<Student> studentList = new List<Student>();
public void AddStudent(Student s) { myArrayList.Add(s); studentList.Add(s); }
protected void TestArrayList() { Debug.WriteLine("TestArrayList"); Debug.WriteLine("Items:"); foreach (Student item in myArrayList) { Debug.WriteLine(item); } // this works, with the Equals set up Debug.WriteLine(myArrayList.IndexOf("Anna Martinsson"));
// this doesn't work, and shouldn't Debug.WriteLine(myArrayList.IndexOf("Mikael")); }
//Debug.WriteLine(studentList.IndexOf("Mikael")); // here we write a custom search for what we want Debug.WriteLine(studentList.FindIndex(delegate(Student item) { return item.Name.StartsWith("Mikael"); })); }
public int CompareTo(object obj) { if (obj==null) { return -1; } if (this.GetType()==obj.GetType()) { Student e = (Student)obj; return this.name.CompareTo(e.name); // ascending } return this.name.CompareTo(obj.ToString()); // ascending }
// overide Equals, to lookup by name. public override bool Equals(object obj) { return this.CompareTo(obj) == 0; }
// if you override Equals, you must override hash code // just give it the hashCode of name and it's fine public override int GetHashCode() { return this.name.GetHashCode(); }
if i for example also wanted to search for an cource do i need to add another CompareTo ? or do i then need an function called searchCource but isen't then CompareTo only checking name not cource? any tutorials where i can find more information about this? stupid questions, but i'm trying to learn