//check theo mssv, update phan tu tim thay theo s public void Update(SV s) { for (int i = 0; i < this.n; i++) { SV temp = this.arr[i]; if (temp.MSSV == s.MSSV) { temp.NameSV = s.NameSV; temp.Age = s.Age; this.arr[i] = temp; } } }
static void Main(string[] args) { SV sv1 = new SV { MSSV = "102", NameSV = "TDD", Age = 21, }; SV sv2 = new SV { MSSV = "103", NameSV = "BQK", Age = 22, }; SV sv3 = new SV { MSSV = "104", NameSV = "NNQ", Age = 23, }; QLSV db = new QLSV(); db.Add(sv1); db.Add(sv2); db.Insert(sv3, 1); db.ToString(); Console.WriteLine("Index of SV2: " + db.IndexOf(sv2)); Console.WriteLine("Removing sv3"); db.Remove(sv3); db.ToString(); Console.WriteLine("Updating sv2"); SV sv4 = new SV { MSSV = "103", NameSV = "Khoi Bui", Age = 25, }; db.Update(sv4); db.ToString(); }
//Delete sv in K public void RemoveAt(int k) { SV[] newarr = new SV[n - 1]; int idx = 0; for (; idx < k; idx++) { newarr[idx] = this.arr[idx]; } for (; idx < n - 1; idx++) { newarr[idx] = this.arr[idx + 1]; } this.arr = newarr; this.n -= 1; }
//insert public void Insert(SV s, int k) { SV[] newarr = new SV[n + 1]; int idx = 0; for (; idx < k; idx++) { newarr[idx] = this.arr[idx]; } newarr[idx] = s; idx += 1; for (; idx < n + 1; idx++) { newarr[idx] = this.arr[idx - 1]; } this.arr = newarr; this.n += 1; }
//Find and delete public void Remove(SV s) { int idx = this.IndexOf(s); this.RemoveAt(idx); }
//add vao cuoi mang public void Add(SV s) { this.Insert(s, n); }