简体   繁体   English

如何在数组中多次声明相同的字符串值?

[英]How to declare the same string value multiple times inside an array?

My question is in relation to multiple array values that are the same within C#. 我的问题与C#中相同的多个数组值有关。 As a rosary has five decades, I want to make values "arr[1,2,...10, 14,16,...23]="Hail Mary";" 由于念珠有五十年,我想使值成为“ arr [1,2,... 10,14,16,... 23] =“冰雹玛丽”;“ I could just write each value out in the string array, but feel reducing the size of code would make the program more efficient in limiting its size. 我可以将每个值写到字符串数组中,但是感觉减少代码的大小将使程序在限制其大小方面更加有效。 Most questions that deal with repetitious values tend to be asked to avoid such, but this deals with different circumstances and a different type such as int. 多数涉及重复值的问题往往会被要求避免这种情况,但这会涉及不同的情况和不同的类型,例如int。 Mine is just the opposite where I am using string and desire to repeat the same value in multiple index numbers. 我的正好相反,我使用字符串,并希望在多个索引号中重复相同的值。 I think there can be multiple uses such as a windows form program that repeats step-by-step instructions to a user to follow that might display a repeated string value. 我认为可以有多种用途,例如Windows窗体程序,它会向用户重复执行逐步的说明,从而可能会显示重复的字符串值。

1)is there a way to code an array with multiple same values, or do I just write each value out as my example already does? 1)有没有一种方法可以对具有多个相同值的数组进行编码,还是像我的示例那样将每个值都写出来?

2)If using string[] arr={"Our Father", "Hail Mary", "Hail Mary",..., "Glory Be", "O My Jesus"}; 2)如果使用string[] arr={"Our Father", "Hail Mary", "Hail Mary",..., "Glory Be", "O My Jesus"}; instead of my example that declares each indexed string value, how would it be coded to insert the same value inside the declared string array? 而不是我的示例声明每个索引的字符串值,而是如何将其编码为在声明的字符串数组中插入相同的值?

int counter=0;

private void button1_Click(Object sender, EventArgs e)
    {  string[] arr= new string[12]; 
arr[0]="Our Father...";
arr[1]="Hail Mary...";
arr[2]="Hail Mary...";
...
arr[11]="Glory Be...";
arr[12]="O My Jesus...";

textBox1.Text = arr[counter];
counter++;
textBox1.Text = arr[counter-1].ToString();
}

Use Enumerable.Repeat method for this 为此使用Enumerable.Repeat方法

var array = Enumerable.Repeat("Hail Mary", count).ToArray();

This will create an array of count items where each of them will be equal to "Hail Mary". 这将创建一个count项目数组,其中每个count项目均等于“冰雹玛丽”。 Then just place different values where they should be. 然后只需将不同的值放在应有的位置即可。

array[0] = "Our Father";

For more information about this method check out MSDN . 有关此方法的更多信息,请查看MSDN

Maybe you can write a simple method for your scenario: 也许您可以为您的方案编写一个简单的方法:

public void SetValuee(int start,int end,string value,ref string[] array)
{
   for(int i=start;i<=end;i++)
   { 
       array[i] = value;
   }
}

And use: 并使用:

SetValues(1,12,"Hail Mary...",ref arr)

You can use switch statement for such purpose: 您可以将switch语句用于以下目的:

string[] arr= new string[23];
for (int i = 0; i < arr.Length; i++) {
    switch (i) {
    case 0:
        array[i] = "Our Father...";
        break;
    case 11:
        arr[11]="Glory Be...";
        break;
    case 11:
        arr[12]="O My Jesus...";
        break;
    default:
        array[i] = "Hail Mary";
        break;
    }
}

The sense is that the most common entry would be "Hail Mary" , you put it as a default case and process the rest of the cases separately. 从某种意义上说,最常见的条目将是"Hail Mary" ,您将其作为默认案例,然后分别处理其余案例。

Note also I'm using arr.Length instead of just number; 另请注意,我使用的是arr.Length而不是数字; I'd recommend you to always do that as a good practice (to not repeat same constants over and over). 我建议您始终将此作为一种好习惯(不要一遍又一遍地重复相同的常数)。

 string[] arr = { "Our Father", "Hail Mary", "Hail Mary", "Glory Be", "O My Jesus" };
 var list = arr.ToList();
 list.Insert(2, "Hail Mary");
 arr = list.ToArray();

Yet another answer, you could use a second array and a foreach loop 另一个答案是,您可以使用第二个array和一个foreach循环

int[] index = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
string[] arr = new string[13];

foreach (int i in index)
{
    arr[i] = "Hail Mary...";
}
arr[0] = "Our Father...";
arr[11] = "Glory Be...";
arr[12] = "O My Jesus...";

No need to overcomplicate things, just assign the value to a string variable and then use it when you declare the array. 无需过于复杂,只需将值分配给字符串变量,然后在声明数组时使用它即可。 you will not gain anything in performance by somehow reducing the size of source code, or by using a "clever" method that would populate your array. 通过某种方式减小源代码的大小或使用填充数组的“聪明”方法,您将不会获得任何性能上的提高。 If there are thousands or more places where you'd need to put your string that repeats, you could perhaps gain a few milliseconds, but you'd lose in clarity of what you're doing. 如果有成千上万个地方需要重复放置字符串,则可能会花费几毫秒的时间,但您可能会不清楚所执行的操作。

So, for the second part of your question, you could declare the array like this: 因此,对于问题的第二部分,您可以这样声明数组:

string repeatedString = "I'm repeating";
string[] arr = 
{ 
    "Value 1", 
    "Another string", 
    repeatedString, 
    "some more", 
    repeatedString, 
    repeatedString, 
    "Yet another" 
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM