Java vs. C#

Array Type


Java
 
Java has two kinds of arrays:
  • single-dimensional
  • multi-dimensional or jagged arrays (arrays of arrays).
Note that the declaration has big difference from C# int[] myIntArray = new int[5]; //create a single-dimensional array int[] myIntArray = { 2, 4, 6, 8, 10 }; // initialize an array int[,] myRectangularArray = new int[rows, columns]; //illegal int[,] myRectangularArray = { {0,1,2}, {3,4,5}, {6,7,8}, {9,10,11} //illegal }; int [,,] array = new int [3, 4, 5];//illegal int [1,1,1] = 5;//illegal int [][][] array = new int [3][4][5]; // ok int [1][1][1] = 5; //ok
class Test { public static void main(String[] args) { int[] a1; // single-dimensional array of int int[][] a2; // 2-dimensional array of int int[][][] a3; // 3-dimensional array of int int[][] j2; // "jagged" array: array of (array of int) int[][][] j3; // array of (array of (array of int)) } }
class Test { public static void main(String[] args) { int[] a1 = new int[] {1, 2, 3}; int[][] a2 = new int[][] {{1, 2, 3}, {4, 5, 6}}; int[][][] a3 = new int[10][20][30]; int[][] j2 = new int[3][]; j2[0] = new int[] {1, 2, 3}; j2[1] = new int[] {1, 2, 3, 4, 5, 6}; j2[2] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; } }
class Test { public static void main(String[] args) { int[] arr = new int[5]; for (int i = 0; i < arr.length; i++) arr[i] = i * i; for (int i = 0; i < arr.length; i++) System.out.println("arr[" + i + "] = " + arr[i]); } }
int[][] jag = new int[][] {new int[] {1, 2, 3, 4}, new int[] {5, 6, 7, 8, 9, 10}}; //access elements for (int i = 0; i < jag.length; i++) for (int j = 0; j < jag[i].length; j++) System.out.println(jag[i][j]);

C#
 
C# has three kinds of arrays:
  • single-dimensional
  • multi-dimensional rectangular arrays
  • jagged arrays (arrays of arrays).
Note that the declaration has big difference from Java int[] myIntArray = new int[5]; //create a single-dimensional array int[] myIntArray = { 2, 4, 6, 8, 10 }; // initialize an array int[,] myRectangularArray = new int[rows, columns]; //create a rectangular array int[,] myRectangularArray = { {0,1,2}, {3,4,5}, {6,7,8}, {9,10,11} //intialize array }; int [,,] array = new int [3, 4, 5]; // creates a 3x4x5 array int [1,1,1] = 5; int [][][] array = new int [3][4][5]; // creates 1+3+12=16 arrays int [1][1][1] = 5;
class Test { static void Main() { int[] a1; // single-dimensional array of int int[,] a2; // 2-dimensional array of int int[,,] a3; // 3-dimensional array of int int[][] j2; // "jagged" array: array of (array of int) int[][][] j3; // array of (array of (array of int)) } }
class Test { static void Main() { int[] a1 = new int[] {1, 2, 3}; int[,] a2 = new int[,] {{1, 2, 3}, {4, 5, 6}}; int[,,] a3 = new int[10, 20, 30]; int[][] j2 = new int[3][]; j2[0] = new int[] {1, 2, 3}; j2[1] = new int[] {1, 2, 3, 4, 5, 6}; j2[2] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; } }
class Test { static void Main() { int[] arr = new int[5]; for (int i = 0; i < arr.Length; i++) arr[i] = i * i; for (int i = 0; i < arr.Length; i++) Console.WriteLine("arr[{0}] = {1}", i, arr[i]); } }
int[][] jag = new int[][] {new int[] {1, 2, 3, 4}, new int[] {5, 6, 7, 8, 9, 10}}; for (int i = 0; i < jag.Length; i++) for (int j = 0; j < jag[i].Length; j++) System.Console.WriteLine(jag[i][j]); or for (int i = 0; i < jag.GetLength(0); i++) for (int j = 0; j < jag[i].GetLength(0); j++) System.Console.WriteLine(jag[i][j]);