0

我知道这是一个简单的问题,但它困扰了我一整天。我尝试通过方法(setLength)中的变量设置数组的长度。但是我调用方法之后setLength,我的数组长度还是0,不是我调用方法的时候设置为5setLength吗?

C#代码片段:

class myProject 
{
   public static int length = 0;
   public static string[] myArray1 = new string[length];
   public static string[] myArray2 = new string[length];

public static void setLength()
{
  length = 5;
}

public static void process()
{
  setLength();

    for(int i=0; i<length; i++)
     {
       .....code for my array
     }
}
4

4 回答 4

6

您必须创建一个新数组。

public static void setLength()
{
    length = 5;
    myArray1 = new string[length];
}

你不能随意改变长度。c# 数组不支持。

如果您想使用可变长度结构,我建议使用List<string>

于 2012-12-06T03:08:13.220 回答
5

数组的大小是在构造对象时使用长度变量的值设置的。数组中没有创建变量关系:大小设置一次,之后固定。

如果您想要一个改变大小的数组,请使用 List<string> 之类的集合类型。

于 2012-12-06T03:09:06.900 回答
2

由于myArray1myArray2都被定义为长度等于length,因此除非您更改它,否则它们的长度将始终为 ,因为 IDE 逐行编译项目。length00

但是,如果您愿意更改数组长度,您可以使用Array.Resize<T>(ref T[] array, int newSize)wherearray数组来调整大小,并且newSize是数组的新长度。

例子

class myProject 
{
   public static int length = 0; //Initialize a new int of name length as 0
   public static string[] myArray1 = new string[length]; //Initialize a new array of type string of name myArray1 as a new string array of length 0
   public static string[] myArray2 = new string[length]; //Initialize a new array of type string of name myArray2 as a new string array of length 0

public static void setLength()
{
    length = 5; //Set length to 5
}

public static void process()
{
    setLength(); //Set length to 5
    Array.Resize(ref myArray1, length); //Sets myArray1 length to 5
    //Debug.WriteLine(myArray1.Length.ToString()); //Writes 5
}

注意:在大多数情况下,最好使用System.Collections.Generic.List<T>它,因为它更易于管理和动态调整大小。

例子

List<string> myList1 = new List<string>(); //Initializes a new List of string of name myList1
private void ListItems()
{
    AddItem("myFirstItem"); //Calls AddItem to add an item to the list of name myFirstItem
    AddItem("mySecondItem"); //Calls AddItem to add an item to the list of name mySecondItem
    AddItem("myThirdItem"); //Calls AddItem to add an item to the list of name myThirdItem

    string[] myArray1 = GetAllItemsAsArray(myList1); //Calls GetAllItemsAsArray to return a string array from myList1

 /* foreach (string x in myArray1) //Get each string in myArray1 as x
    {
        MessageBox.Show(x); //Show x in a MessageBox
    }   */
}
private void RemoveItem(string name)
{
    myList1.RemoveAt(myList1.IndexOf(name)); //Removes an item of name "name" from the list using its index
}
private void AddItem(string name)
{
    myList1.Add(name); //Adds an item of name "name" to the list
}
private string[] GetAllItemsAsArray(List<string> list)
{
    string[] myArray = new string[list.Count]; //Initialize a new string array of name myArray of length myList1.Count
    for (int i = 0; i < list.Count; i++)  //Initialize a new int of name i as 0. Continue only if i is less than myList1.Count. Increment i by 1 every time you continue
    {
        myArray[i] = list[i]; //Set the item index of i where i represents an int of myArray to the corresponding index of int in myList1
    }

    return myArray; //Return myArray
}

谢谢,
我希望你觉得这有帮助:)

于 2012-12-06T03:14:34.057 回答
1

您应该做的不是在类声明中初始化数组的大小,而是在 setLength 方法中对其进行初始化。前面的答案状态的问题是,一旦你初始化了你的数组,调整它的大小是一项昂贵的操作。如果这将是您不止一次做的事情,您需要考虑使用List<string>.

class myProject 
{
   public static int length = 0;
   public static string[] myArray1;
   public static string[] myArray2;

   public static void setLength(int value)
   {
       myArray1 = new string[value];
       myArray2 = new string[value];
   }

   public static void process()
   {
      length = 5;
      setLength(length);

      for(int i=0; i<length; i++)
      {
         .....code for my array
      }
   }
}
于 2012-12-06T03:21:56.380 回答