Advertisement
jyoung12387

Jagged Array

Feb 17th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. using System;
  2.  
  3. namespace random
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int[][] jaggedArray = new int[3][];
  10.  
  11.            
  12.             //declaring jaggedArray without specifying values, just the size of the arrays.
  13.             jaggedArray[0] = new int[5];
  14.             jaggedArray[1] = new int[3];
  15.             jaggedArray[2] = new int[2];
  16.  
  17.             //declaing jaggedArry and specifying values without stating the size of array.
  18.             jaggedArray[0] = new int[] { 2, 3, 5, 7, 11 };
  19.             jaggedArray[1] = new int[] { 1, 2, 3 };
  20.             jaggedArray[2] = new int[] { 13, 21 };
  21.  
  22.             // alternative method:
  23.  
  24.             int[][] jaggedArray2 = new int[][]
  25.             {
  26.                 new int[] { 2, 3, 5, 7, 11 },
  27.                 new int[] { 1, 2, 3 },
  28.                 new int[] { 13, 21 }
  29.             };
  30.  
  31.             Console.WriteLine($"The value in the middle of the first entry is {jaggedArray2[0][2]}\n");
  32.  
  33.             //Print to Console all values in jaggedArray2
  34.  
  35.             for (int i = 0; i< jaggedArray2.Length; i++)    //first for loop is getting the 1st, 2nd, and 3rd array in jaggedArray2
  36.             {
  37.                 Console.WriteLine("Element {0}", i);
  38.                 for (int j = 0; j < jaggedArray2[i].Length; j++)
  39.                     Console.WriteLine("{0} ", jaggedArray2[i][j]); //position 'i', which is going to be 0, 1 or 2
  40.             }                                                      //position 'j', which is going to be 0-4, 0-2, then 0-1
  41.  
  42.             Console.ReadLine();
  43.         }
  44.  
  45.  
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement