3

Can a two-dimensional array of primitive data type contain elements of different types? I have read articles that suggest it both can and cannot.

Geeg
  • 97
  • 4
  • 3
    No, java is strongly typed. However, `Object[]` can [technically contain multiple types](http://stackoverflow.com/a/10007820/3928341) which you can typecast when adding to/removing from the array – nem035 Feb 02 '16 at 19:53
  • Can you provide a link to an example article that suggests it can? – mellamokb Feb 02 '16 at 19:55
  • Actually, you can store all primitive values in "widest" types i. e. long and double using bit arithmetic (except returnAddress, of course). – Ιναη ßαbαηιη Feb 02 '16 at 19:58
  • @balalaika The only reason a variable of type `int` seems assignment-compatible with a reference of type `long` is because Java will implicitly auto-cast from smaller types to wider types since there is no potential for loss of data. Just wanted to clarify. – Parker Hoyes Feb 02 '16 at 20:05

3 Answers3

2

A two-dimensional array of a primitive type such as int isn't technically only of primitive types. The outer array contains an array of int[] arrays, which are in fact Objects - not a primitive type (int[] is a subtype of Object).

This means that an int[][] array could contain null, while the inner int[] arrays can only contain primitive ints. An int[][] array cannot however, contain an element of any type other than int[].

Demonstration

This code compiles and executes with no exceptions:

int[][] a = {{1, 2, 3}, {4, 5, 6}, null};
System.out.println(Arrays.deepToString(a));
Object b = a;
System.out.println(Arrays.deepToString((int[][]) b));
int[][] c = a;
System.out.println(Arrays.deepToString(c));
int[] d = a[1];
System.out.println(Arrays.toString(d));
Object e = d;
System.out.println(Arrays.toString((int[]) e));
int[] f = a[2];
System.out.println(Arrays.toString(f));

And will output:

[[1, 2, 3], [4, 5, 6], null]
[[1, 2, 3], [4, 5, 6], null]
[[1, 2, 3], [4, 5, 6], null]
[4, 5, 6]
[4, 5, 6]
null
Parker Hoyes
  • 2,118
  • 1
  • 23
  • 38
0

In response to Nem's comment, I'd suggest you want to have a 2d array of a common interface, that way the container (2d array) can hold the different types. It would make more sense to another programmer when seeing the array of that interface, rather than an array of Object. Also using the "instanceof" operator to check the specific implementation of said interface.

I can provide an example if needed.

Greg Hilston
  • 2,397
  • 2
  • 24
  • 35
0

Can a two-dimensional array of primitive data type contain elements of different types? I have read articles that suggest it both can and cannot

Using int array as an example:

int[] nums1;     //Array of integers
int[][] nums2;   //Array of (Array of integers)
int[][][] nums3; //Array of (Array of (Array of integers))

So if you perceive 2D arrays as a table with rows and columns are if you are asking whether the table can contains different datatype in different rows, the answer is no.

But since multi-dimensional arrays are merely array of array(s), in actual fact, they are indeed holding data of different types: one is holding int the other is holding an array (an object).

user3437460
  • 17,253
  • 15
  • 58
  • 106