首先,我需要从DataGridView.SelectedCells.Values
. 然后我需要将该字符串附加到自身,直到member.count
达到限制。例如,如果
string [] = {"a", "b", "c"}; // Where abc are selectedCells.Values.
新的字符串 [] 应该是:
{"a", "b", "c", "a", "b", "c", "a", "b"}
- 例如,如果限制为 8。
请问我怎么解决这个问题?
首先,我需要从DataGridView.SelectedCells.Values
. 然后我需要将该字符串附加到自身,直到member.count
达到限制。例如,如果
string [] = {"a", "b", "c"}; // Where abc are selectedCells.Values.
新的字符串 [] 应该是:
{"a", "b", "c", "a", "b", "c", "a", "b"}
请问我怎么解决这个问题?
您可以%
在 for 循环中使用 (Modulus)。
string[] oldArr = new string[3] {"a","b","c"};
string[] newArr = new string[8];
int limit = 8;
for ( int i = 0 ; i < limit ; i++ )
{
newArr[i] = oldArr[i%oldArr.Length];
}
就是这样。
只需为原始数组索引保留一个单独的计数器。就像是:
string[] strings = new string[] { "a", "b", "c" };
string[] final = new string[8];
int index = 0;
for(int i = 0;i < 8;++i)
{
final[i] = strings[index];
index = (index + 1) % strings.Length;
}
您可以尝试使用类似的东西
int yourLimit = 8;
int yourIndexer = 0;
string[] strArr = new string[3] { "a", "b", "c" };
List<string> list = new List<string>();
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
if (strArr.Contains(cell.Value.ToString()) && yourIndexer < yourLimit)
list.Add(cell.Value.ToString());
yourIndexer++;
}
string[] strNewArr = list.ToArray<string>();
我希望这有帮助。
此版本用于Array.Copy
复制。
{
int N=10;
string[] strings=new string[] { "a", "b", "c" };
int L=strings.Length;
int R = (int) Math.Ceiling(N/(1.0*L));
string[] result=new string[N];
for(int index=0; index<R; index++)
{
int offset = index*L;
Array.Copy(strings, 0, result, offset, Math.Min(L, N-offset));
}
}