Home > Blockchain >  I'm confused about jagged arrays. I'm trying to make a for loop while accessing, print or
I'm confused about jagged arrays. I'm trying to make a for loop while accessing, print or

Time:10-12

This is what I have so far. I'm trying to access and change the values

int[][] numbers = new int[][]{ //giving the values...
new int[]{4,5,6,7},
new int[]{6,88,9},
new int[]{4}
};

for (i = 1; i <= n; i  )
{
   'change value, access value or print values...'
}

CodePudding user response:

In Jagged Array, you can point to the array using the first index, and with the second index point to the number in each array. Try this.

using System;
                    
public class Program
{
    public static void Main()
    {
        int[][] numbers = new int[][]{ //giving the values...
            new int[]{4,5,6,7},
            new int[]{6,88,9},
            new int[]{4}
        };

        for (var i = 0; i < numbers.Length; i  )
        {
           for (var j = 0; j < numbers[i].Length; j  )
           {
               Console.Write(numbers[i][j]   "\t");
           }
            Console.WriteLine("");
        }
    }
}

CodePudding user response:

simplest way for access and print value and you can change value in item variable.

    foreach (var sub_arr in numbers)
    {
        foreach (var item in sub_arr)
            Console.Write(item   "\t");
        Console.WriteLine("-");
    }
  • Related