Sunday, February 15, 2015

C# vs Java : arrays

Both C# and Java support creating and working with arrays. There are a few differences between the two languages though.
how arrays are declared
When declaring an array variable Java doesn't care if you put the square brackets before or after the identifier.
int[] first;
int second[];
The preferred way is to place them before the identifier. This is actually the only way you're allowed to declare an array in C#.
public class Klass {    

  public static void Main() {
    int[] first; 

    // int second[];  // compiler error
    // Bad array declarator:
    //  To declare a managed array the rank specifier precedes the variable's identifier.
    //  To declare a fixed size buffer field, use the fixed keyword before the field type.
  }
}
single-dimensional arrays
In C#, unlike Java, you can specify the size of the array when using an array initializer.
int[] array = new int[3] {1, 2, 3};
Note: The above is a compiler error in Java.
jagged arrays
Jagged arrays are arrays that contain arrays. The difference is that each array can vary in size. Both C# and Java support the same syntax for declaring a jagged array.
int[][] array = new int[3][];
Likewise they both support various ways to initialize the array upon declaration.

In C# you can specify the number of arrays, but in Java you can't.
// This is valid in C#, but not Java.
int[][] array = new int[2][] {
  new int[] {1,2,3}, 
  new int[] {4,5,6} 
};
Both C# and Java support the following two forms.
int[][] array = new int[][] {
  new int[] {2,3,4},
  new int[] {5,6,7,8,9}
};

int[][] array = { 
  new int[] {2,3,4}, 
  new int[] {5,6,7,8,9} 
};
Java supports an even shorter version of this that is not supported in C#.
// This is valid in Java, but not C#.
int[][] array = {
  {1, 2, 3},
  {10, 20, 30}
};
multi-dimensional arrays
C# supports a third type of arrays which are known as multidimensional arrays. Multi-dimensional arrays are like jagged arrays except that the length of each array is the same. Multi-dimensional arrays are sometimes known as rectangular arrays.

Here's how to declare a two-dimensional rectangular array.
int[,] array;
Here's how to initialize a two-dimensional rectangular array where each array contains two elements.
array = [2,2];
You can also specify the values for each array upon initialization.
int[,] array = new int[2,2] {
  {1, 2},
  {3, 4}
};
You can even omit the size of the array, like so:
int[,] array = new int[,] {
  {1, 2}
  {3, 4}
};
An even terser form exists where the new operator is left off.
int[,] array = {
  {1, 2}
  {3, 4}
};
Finally, here's how to set the value for one of the array's elements.
array[1,1] = 123;
Note: Java does not support this type of array.
length of array
Java arrays have a length field which begins with lower-case letter.
int[] array = {0, 1, 2, 3};
System.out.println("array.length = " + array.length); // array.length = 4
C#, on the other hand, has a length property and it begins with an upper-case letter.
int[] array = new int[3] {0, 1, 2, 3};
System.Console.WriteLine("array.Length = " + array.Length); // array.Length = 4
This is the same syntax you would use to get the length of a string in C#.
string str = "Hello World";
System.Console.WriteLine("str.Length = " + str.Length); // str.Length = 11
To get the length of a string in Java you use the length method.
String str = "Hello World";
System.out.println("str.length() = " + str.length()); // str.length() = 11
variable number of arguments
Both C# and Java have a varargs feature which allows you to pass a variable number of arguments to a method without having to put them in an array first. In Java this is specified by using three dots.
public static void fooBar(int... array) {
  // ... stuff
}
C# you use a params modifier, like so:
public static void fooBar(params int[] array) {
  // ... stuff
}

See Also: My post on Java arrays

No comments:

Post a Comment