1. 程式人生 > >怎樣程式設計產生 a1,a2,a3,a4變數

怎樣程式設計產生 a1,a2,a3,a4變數

Well, if I am interpreting your question right, you can declare an array or list, then initialize these elements in a loop

For example (array) (if you want a fix number of elements):

int n =10;// number of stringsstring[] str =newstring[n];// creates a string array of n elementsfor(int i =0; i < n;
i++){ str[i]="";// set the value "" at position i in the array}

(list) (if you don't want a fix number of elements)

using System.Collections.Generic;...int n =10;List<string> str =newList<string>();// creates a list of strings// List<string> str = new List<string>(n) to set the number it can hold initially (better performance)
for(int i =0; i < n; i++){list.Add("");// if you've set an initial capacity to a list, be aware that elements will go after the pre allocated elements}list[0]="hello world";// how to use a Listlist[list.Count-1]="i am the last element";// list.Count will get the total amount of