使用 "template" 向数组添加值
Add values to array using "template"
我不确定标题是否准确描述了我想要实现的目标。
简单地说:
如果我有 6 个输入:
- Input1: String1
- Input2: String1Count
- Input3: String2
- Input4: String2Count
- Input5: String3
- Input6: String3Count
我想按字符串各自计数指定的次数将每个字符串添加到数组中。完成此操作后,数组中仍有字段为空,再次循环值并重复它直到数组已满。
像这样
public string str1 = "First String";
public int String1Count = 4;
public string str2 = "Second String";
public int String2Count = 7;
public string str3 = "Third String";
public int String3Count = 5;
int[] arr1 = new int[100];
for (int i = 0; i < arr1.Length; i++)
{
arr1[i] = str1;
arr1[i+1] = str1;
arr1[i+2] = str1;
arr1[i+3] = str1;
arr1[i+4] = str1;
arr1[i+5] = str2;
arr1[i+6] = str2;
arr1[i+7] = str2;
...
arr1[arr1.lenght] = str; //It may be str1, str2 or str3, whichever is next
}
我认为至少需要一个 for loop
和一些 if
语句,但我想不通。
首先,您不能将 string
分配给 int[]
数组。您必须将其分配给 string[]
数组。要解决您的问题,您可以使用模 %
运算符:
string str1 = "First String";
int String1Count = 4;
string str2 = "Second String";
int String2Count = 7;
string str3 = "Third String";
int String3Count = 5;
string[] arr1 = new string[100];
int sum12 = String1Count + String2Count;
int sum123 = String1Count + String2Count + String3Count;
for (int i = 0; i < arr1.Length; i++)
{
if (i % sum123 < String1Count)
arr1[i] = str1;
else if (i % sum123 < sum12)
arr1[i] = str2;
else if (i % sum123 < sum123)
arr1[i] = str3;
}
可能是这样的:
string str1 = "First String";
int String1Count = 4;
string str2 = "Second String";
int String2Count = 7;
string str3 = "Third String";
int String3Count = 5;
var resultList = new List<String>();
resultList.AddRange(Enumerable.Repeat(str1, String1Count));
resultList.AddRange(Enumerable.Repeat(str2, String2Count));
resultList.AddRange(Enumerable.Repeat(str3, String3Count));
string[] resultArray = resultList.ToArray();
我不确定标题是否准确描述了我想要实现的目标。 简单地说:
如果我有 6 个输入:
- Input1: String1
- Input2: String1Count
- Input3: String2
- Input4: String2Count
- Input5: String3
- Input6: String3Count
我想按字符串各自计数指定的次数将每个字符串添加到数组中。完成此操作后,数组中仍有字段为空,再次循环值并重复它直到数组已满。
像这样
public string str1 = "First String";
public int String1Count = 4;
public string str2 = "Second String";
public int String2Count = 7;
public string str3 = "Third String";
public int String3Count = 5;
int[] arr1 = new int[100];
for (int i = 0; i < arr1.Length; i++)
{
arr1[i] = str1;
arr1[i+1] = str1;
arr1[i+2] = str1;
arr1[i+3] = str1;
arr1[i+4] = str1;
arr1[i+5] = str2;
arr1[i+6] = str2;
arr1[i+7] = str2;
...
arr1[arr1.lenght] = str; //It may be str1, str2 or str3, whichever is next
}
我认为至少需要一个 for loop
和一些 if
语句,但我想不通。
首先,您不能将 string
分配给 int[]
数组。您必须将其分配给 string[]
数组。要解决您的问题,您可以使用模 %
运算符:
string str1 = "First String";
int String1Count = 4;
string str2 = "Second String";
int String2Count = 7;
string str3 = "Third String";
int String3Count = 5;
string[] arr1 = new string[100];
int sum12 = String1Count + String2Count;
int sum123 = String1Count + String2Count + String3Count;
for (int i = 0; i < arr1.Length; i++)
{
if (i % sum123 < String1Count)
arr1[i] = str1;
else if (i % sum123 < sum12)
arr1[i] = str2;
else if (i % sum123 < sum123)
arr1[i] = str3;
}
可能是这样的:
string str1 = "First String";
int String1Count = 4;
string str2 = "Second String";
int String2Count = 7;
string str3 = "Third String";
int String3Count = 5;
var resultList = new List<String>();
resultList.AddRange(Enumerable.Repeat(str1, String1Count));
resultList.AddRange(Enumerable.Repeat(str2, String2Count));
resultList.AddRange(Enumerable.Repeat(str3, String3Count));
string[] resultArray = resultList.ToArray();