In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the programming development of linear linked list how to test, the article is very detailed, has a certain reference value, interested friends must read it!
We can add some integer values to the linear linked list test:
Public void Run () {LinkedList ll = new LinkedList (); for (int I = 0; I < 10; I + +) {ll.Add (I);} Console.WriteLine (ll); Console.WriteLine ("Done. Adding employees...");}
If you test this code, it will work as expected:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Done. Adding employees...
However, because this is a collection of Object types, you can also add Employee types to the collection.
Ll.Add (new Employee ("John")); ll.Add (new Employee ("Paul")); ll.Add (new Employee ("George")); ll.Add (new Employee ("Ringo")); Console.WriteLine (ll); Console.WriteLine ("Done.")
The output confirms that both the integer value and the Employee type are stored in the same collection.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Done. Adding employees... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, John, Paul, George, Ringo Done.
While this may seem convenient, the downside is that you lose all types of safety features. Because a linear linked list requires an Object type, each integer value added to the collection is implicitly boxed, as shown in the IL code:
IL_000c: box [mscorlib] System.Int32 IL_0011: callvirt instance void ObjectLinkedList.LinkedList::Add (object)
Similarly, as mentioned above, when you take items from your list, these integers must be explicitly unboxed (cast to integers) and Employee types must be cast to Employee types.
Console.WriteLine ("The fourth integer is" + Convert.ToInt32 (ll [3])); Employee d = (Employee) ll [11]; Console.WriteLine ("The second Employee is" + d)
The solution to these problems is to create a type-safe collection. An Employee linear linked list will not accept Object types; it only accepts instances of the Employee class (or inherits from instances of the Employee class). This will be type-safe and will no longer require type conversions. An integer linear linked list that will no longer require boxing and unboxing (because it can only accept integer values).
As an example, you will create an EmployeeNode that knows that its data type is Employee.
Public class EmployeeNode {Employee employeedata; EmployeeNode employeeNext;}
The Append method now accepts a parameter of type EmployeeNode. You also need to create a new EmployeeLinkedList, and the linked list accepts a new EmployeeNode:
Public class EmployeeLinkedList {EmployeeNode headNode = null;}
The EmployeeLinkedList.Add () method no longer accepts an Object, but accepts an Employee:
Public void Add (Employee data) {if (headNode = = null) {headNode = new EmployeeNode (data);} else {headNode.Append (new EmployeeNode (data));}}
Similarly, indexers must be modified to accept EmployeeNode types, and so on. This does solve the problem of packing and unpacking, and adds type safety features. You can now add Employee (but not integers) to your new linear linked list, and when you extract Employee from it, type conversions are no longer needed.
EmployeeLinkedList employees = new EmployeeLinkedList (); employees.Add (new Employee ("Stephen King")); employees.Add (new Employee ("James Joyce")); employees.Add (new Employee ("William Faulkner")); / * employees.Add (5); / / try to add an integer-won't compile * / Console.WriteLine (employees); Employee e = employees [1]; Console.WriteLine ("The second Employee is" + e)
How nice! when an integer tries to implicitly convert to an Employee type, the code doesn't even pass the compiler!
But the downside is that every time you need to create a type-safe list, you need to do a lot of copy / paste. It's not good enough. There's no code reuse. At the same time, if you are the author of this class, you can't even know in advance what types the link list should accept, so you have to leave the job of adding type safety to the users of the class-your users.
These are all the contents of the article "how to Test Linear linked lists in programming Development". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.