Upload
adie
View
29
Download
0
Embed Size (px)
DESCRIPTION
Lecture 11:. Generics. using System.IO; : : namespace EmpListDemo { static class Program { static void Main( string [] args) { string fname = "empstable.txt" ; string txtline; - PowerPoint PPT Presentation
Citation preview
Lecture 11:
Generics
The Generic List<T>loading data from a file
using System.IO; : :namespace EmpListDemo{ static class Program { static void Main(string[] args) { string fname = "empstable.txt"; string txtline;
List<Employee> Emps = new List<Employee>(); // a generic list of Employee List<Employee> Emps2 = new List<Employee>(); // we will make a copy of Emps // reading employee data from a text file TextReader tr = new StreamReader(fname); do { txtline = tr.ReadLine(); if (txtline == "xxx") break; string[] field = txtline.Split(','); Emps.Add(new Employee(field[0].Trim(),field[1].Trim(), Convert.ToInt32(field[2]), Convert.ToInt32(field[3]), Convert.ToDouble(field[4])));
} while (true); tr.Close(); : :
// display the contents of the list Empsforeach (Employee emp in Emps){ Console.WriteLine("{0} {1} {2} {3} {4}", emp.FirstName, emp.LastName, emp.Age, emp.YrsEmp, emp.Wage);}
Wade Boggs 45 20 12.5Robin Banks 32 13 9.5Jerry Mander 27 6 8.1Amanda Rekonwith 55 25 22.5Doug Wells 38 10 25.05Anita Break 23 2 7.5Juan Abrew 48 7 32.2Ben Dover 37 9 24.15Ilene Dover 28 1 22.9
Displaying the Contents of the List Emps
// we are making a copy of Emps called Emps2foreach (Employee emp in Emps){ Employee emp2 = new Employee(); emp2 = emp; Emps2.Add(emp2);}// so why not just assign one list to the other?// Emps2 = Emps;//// because this would not make a separate copy but// rather point both Emps2 and Emps to the same records!
Making a Copy of a List
// we "tag" each record that passes our criteriaforeach (Employee emp in Emps2){ if (emp.Age > 39 & emp.YrsEmp >= 10) emp.Tag = true;}
// now we remove all records from Emps2 that HAVE NOT // been "tagged" i.e. remove those with emp.Tag = false// this construct is implemented using a delegateEmps2.RemoveAll(delegate(Employee emp){ return !emp.Tag;});
The RemoveAll Delegate Method
age>39 and yrsemp >= 10Wade Boggs 45 20 12.5Amanda Rekonwith 55 25 22.5
Wade Boggs 45 20 12.5Robin Banks 32 13 9.5Jerry Mander 27 6 8.1Amanda Rekonwith 55 25 22.5Doug Wells 38 10 25.05Anita Break 23 2 7.5Juan Abrew 48 7 32.2Ben Dover 37 9 24.15Ilene Dover 28 1 22.9
// this is a LINQ query!var queryresults = from q in Emps where q.LastName.StartsWith("B") select q;
// so what's in queryresults???// let's take a lookConsole.WriteLine();foreach (var q in queryresults){ Console.WriteLine(q.FirstName + " " + q.LastName);}
Working with LINQ
Wade BoggsRobin BanksAnita Break
Wade Boggs 45 20 12.5Robin Banks 32 13 9.5Jerry Mander 27 6 8.1Amanda Rekonwith 55 25 22.5Doug Wells 38 10 25.05Anita Break 23 2 7.5Juan Abrew 48 7 32.2Ben Dover 37 9 24.15Ilene Dover 28 1 22.9
// let's try another example // notice we don't redefine the var queryresults queryresults = from q in Emps where q.Age>30 select q;
// now what's in queryresults??? Console.WriteLine(); foreach (var q in queryresults) { Console.WriteLine(q.FirstName + " " + q.LastName); } Console.ReadKey();
Another LINQ Query Example
Wade BoggsRobin BanksAmanda RekonwithDoug WellsJuan AbrewBen Dover
Wade Boggs 45 20 12.5Robin Banks 32 13 9.5Jerry Mander 27 6 8.1Amanda Rekonwith 55 25 22.5Doug Wells 38 10 25.05Anita Break 23 2 7.5Juan Abrew 48 7 32.2Ben Dover 37 9 24.15Ilene Dover 28 1 22.9
// one more examplequeryresults =from q in Empswhere ((q.Age>40 & q.Wage<20.0) | (q.Age<40 & q.Wage>25.0))select q;
Wade BoggsDoug Wells
Wade Boggs 45 20 12.5Robin Banks 32 13 9.5Jerry Mander 27 6 8.1Amanda Rekonwith 55 25 22.5Doug Wells 38 10 25.05Anita Break 23 2 7.5Juan Abrew 48 7 32.2Ben Dover 37 9 24.15Ilene Dover 28 1 22.9
5 6 4 3 2 7 7 8 6 54 3 2 5 3 2 3 8 8 81 1 2 2 3 3 6 4 5 33 5 5 6 4 9 7 5 2 04 3 8 7 0 4 3 2 5 41 3 2 4 3 5 4 6 5 77 5 6 8 7 7 5 4 7 91 3 2 4 3 6 5 4 3 28 8 9 6 5 5 3 0 0 11 1 1 6 6 8 8 7 6 5
using System.IO;using System.Collections.Generic;using System.Linq;using System.Text;
namespace LoadArrayFromTextfile{ class Program { static void Main(string[] args) { int[,] mat = new int[10,10]; string textline; int k;
TextReader tr = new StreamReader("sample_01.txt"); for (int i = 0; i < 10; i++) { textline = tr.ReadLine(); k = 0; foreach (string str in textline.Split(' ')) { if (str != "") { mat[i, k] = Convert.ToInt32(str); k += 1; } } } tr.Close();
Loading an Array from a Text File
for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Console.Write("{0} ", mat[i, j]); } Console.WriteLine(); } Console.ReadKey(); } }}
List<Employee> Emps = new List<Employee>();string txtline;
TextReader tr = new StreamReader("employees.txt");
do{ txtline = tr.ReadLine(); if (txtline == "xxx") break; string[] field = txtline.Split('\t'); Emps.Add(new Employee(field[0].Trim(), field[1].Trim(), Convert.ToInt32(field[2]), Convert.ToInt32(field[3]), Convert.ToDouble(field[4])));
} while (true);tr.Close();
Reading and Writing Textfiles
foreach (Employee emp in Emps){ Console.WriteLine("{0} {1}", emp.FirstName, emp.LastName);}
TextWriter tw = new StreamWriter("employees.txt");foreach (Employee emp in Emps){ tw.WriteLine("{0} \t {1} \t {2} \t {3} \t {4}", emp.FirstName, emp.LastName, emp.Age, emp.YrsEmp, emp.Wage);}tw.WriteLine("xxx");tw.Close();
Reading and Writing Text Filescontinued
Dealing with Datacut & paste
Pasting into Word or PPT Preserves Cells
Pasting to a Text Editor Creates Separators e.g. Tabs