How to search an ArrayList
Hey everyone i need some help with creating a
method to search an arraylist. This is going to make me go crazy :(
Ok I have a video store program with a rentalItem class which contains
itemId, title and copies and i have a text document with all the rental
details which is been read and put into rentalList array. User enters a
title they want to search in the textbox and the method searches the
arrayList and returns the line that is found.
//Here is the method
public string QueryMovieItem(string title)
{
rentalList.Sort();
foreach (RentalItem m in rentalList)
{
// i can't figure out what needs to go here. CompareTo?
}
return " ";
}
Here is a sample from the rentalItems.txt
M0001;DVD;The Hours;Nicole Kidman, Meryl Streep;Stephen Daldry;10
M0002;VHS;The Hours;Nicole Kidman, Meryl Streep;Stephen Daldry;10
M0003;DVD;Birthday Girl;Nicole Kidman, Ben Chaplin;Jez Butterworth;8
M0004;VHS;Birthday Girl;Nicole Kidman, Ben Chaplin;Jez Butterworth;8
M0005;DVD;Far and Away;Tom Cruise, Nicole Kidman;Ron Howard;7
M0006;VHS;Far and Away;Tom Cruise, Nicole Kidman;Ron Howard;7
..
..
G0001;PlayStation 2;Lord Of The Rings: Return Of The King;5
G0002;PlayStation 2;MX Unleashed;12
G0003;PlayStation 2;Whiplash;13
G0004;PlayStation 2;Spy Hunter 2;12
G0005;Game Cube;Resident Evil: Code Veronica X;2
G0006;Game Cube;R: Racing Victory;6
G0007;Game Cube;Tak and the Power of Juju;8
G0008;Game Cube;Sonic Heroes;12
G0009;Xbox;Deus Ex: Invisible War;2
G0010;Xbox;Armed And Dangerous;4
and here is how i read the rentalItems.txt into arrayList
public static ArrayList readRentalItems()
{
ArrayList itemList = new ArrayList();
// Process input file
StreamReader sr = new StreamReader(filename2);
String line = sr.ReadLine();
String[] splits;
while (line != null)
{
// Initial test is to echo the input
// Console.Out.WriteLine(line);
// Split out parts and create a member
splits = line.Split(';');
int len = splits.Length;
string fstr = splits[0];
string ch = fstr.Substring(0, 1);
if (ch.Equals("G"))
{
itemList.Add(new Game(splits[0], splits[1],
splits[2], int.Parse(splits[3])));
}
else if (ch.Equals("M"))
{
itemList.Add(new Movie(splits[0], splits[1],
splits[2], splits[3], splits[4], int.Parse(splits[5])));
}
line = sr.ReadLine();
}
return itemList;
}
Please someone help me :)