The string array outputs the index, original value, length, and original value of the array respectively.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.