Speaking of the infinitus classification, I believe many people know what it is and have done it before. I also believe that the most widely used method is to implement recursion.
Recently, I also want to create a menu with unlimited classification, but I don't want to use recursion. So I need to implement it in other ways, that is, iteration.
First, I need to define an entity model. Here is an example of an infinite province/city:
ID { ; PID { ; Name { ; ; } Level { ; ; } }
Then write the method. here we need to take advantage of the features of stack first-in-first-out:
List<Loaction> Soft(List<Loaction> data, = <Loaction> tree = List<Loaction> level = (task.Count > flag = ( i = ; i < data.Count; i++ l = (l.PID ====++--= (! (task.Count > =--
Finally, prepare the data-> call-> output:
Main( data = List<Loaction> Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = Loaction() { ID = , PID = , Name = tree = Soft(data, ( t sb = ( i = ; i < t.Level; i++
Then the effect is as follows:
That's what it looks like ..... (END)