The string array outputs the index, original value, length, and original value of the array respectively.
There is a string array below:
string[] strArr = { "adg45","frwqer","sfd5rtgsd","pdlfd**l","qr$%Ulf5fjk","hlef"};
When you receive this question, how do you solve the write implementation? Write code directly? Or is it necessary to run Object-oriented Thinking for development?
Now that you have this question, the following Insus. NET shares its implementation method:
Create an object that represents the object of each element in the string array:
Class Item {private int _ Index; public int Index {get {return _ Index;} set {_ Index = value;} private string _ Code; public string Code {get {return _ Code;} set {_ Code = value;} private int _ Length; public int Length {get {return _ Length ;} set {_ Length = value ;}} public Item () {} public Item (int index, string code, int length) {this. _ Index = index; this. _ Code = code; this. _ Length = length;} public override string ToString () {return string. format ("Index: {0}, Code: {1}, Length: {2}", _ Index, _ Code, _ Length );}}Source Code
Next, we create another class to process data and store the processed results in a collection:
Class ItemUtility {public List <Item> Items = new List <Item> (); public void Add (int index, string code) {AppendItem (index, code, code. length);} private void AppendItem (int index, string code, int length) {var item = new Item {Index = index, Code = code, Length = length}; Items. add (item );}}Source Code
The above categories are encapsulated in the program for special reference and call by the program.
Users who write programs use the following code:
Class Am {private string [] _ StringArray; public Am (string [] stringArray) {this. _ StringArray = stringArray;} public void Process () {ItemUtility utility = new ItemUtility (); int idx = 0; foreach (string s in _ StringArray) {utility. add (++ idx, s);} var result = utility. items; foreach (Item item in result) {Console. writeLine (item. toString ());}}}Source Code
Then run the above program Am () on the console ()
In the console code, there is only output and output, and there is no need to write too much code here.
Achieve object-oriented programming thinking and encapsulation.