0

これは簡単な質問であることはわかっていますが、1 日中悩まされていました。メソッド (setLength) 内の変数を使用して、配列の長さを設定しようとしています。しかし、メソッドを呼び出した後setLength、配列の長さはまだ 0 です。メソッドを呼び出したときに 5 に設定されていませんsetLengthか?

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

myArray1とはどちらも の長さでmyArray2定義されていたため、 IDE はプロジェクトを 1 行ずつコンパイルするため、変更しない限り、それらの長さは常に同じになります。lengthlength00

ただし、配列の長さを変更したい場合は、サイズを変更する配列のArray.Resize<T>(ref T[] array, int newSize)場所と、配列の新しい長さを使用できます。arraynewSize

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 に答える