Search Unity

Auf eine Dimension eines zweidimensionalen Arrays zugreifen! C#

Discussion in 'Scripting' started by Leo-Prislin, Jan 13, 2019.

  1. Leo-Prislin

    Leo-Prislin

    Joined:
    Jan 13, 2019
    Posts:
    4
    Hey, ich bräuchte dringend Hilfe!
    Ich habe ein zweidimensionales Array in einem C# Script und möchte ein anderes eindimensionales Array mit den Werten der bspw. 1. Dimension des 2-dimensionalen Arrays belegen.

    Code (CSharp):
    1. public class ExampleClass : Monobehaviour {
    2.  
    3. public string[] array1;
    4. public string[,] array2;
    5.  
    6. void Start () {
    7.  
    8. array2 = new string[2, 3] { {"hi", "hey", "hallo"}, {"tschüs", "auf Wiedersehen", "bis dann"} };
    9. array1 = array2[0];
    10.  
    11. }
    12.  
    13. }
    ich dachte, man müsste es so machen, da, wenn array1 das "Begrüßungs-Array" sein soll, man es mit der 0. Dimension von array2 belegen muss. Leider funktioniert das so anscheinend nicht.

    Über hilfreiche Antworten würde ich mich sehr freuen!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I'm sorry, I don't speak German.

    But I hope this helps you. I believe I have solved your problem with a "jagged array."

    All the best!

    Kurt Dekker


    Code (csharp):
    1.     public string[] array1;
    2.     public string[][] array2;
    3.  
    4.     void Start () {
    5.  
    6.         // "jagged array"
    7.         array2 = new string[][] {
    8.             new string[] {"hi", "hey", "hallo"},
    9.             new string[] {"tschüs", "auf Wiedersehen", "bis dann"},
    10.             new string[] {"different", "number", "of", "strings", "many", "different"},
    11.         };
    12.  
    13.         array1 = array2[0];
    14.  
    15.     }
    This is the documentation:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays
     
    Lurking-Ninja likes this.