Roam
Roam

Reputation: 4949

Multiple types on a Java multi-dimensional array

From what I know, only one type of object/literal can be stored on a multi-dimensional Java array.

so for instance, on a 2-dim'l array a[][], I can not store a Type-1 array on a[0] and Type-2 array on a[1] unless there's some polymorphic relationship between Type-1 & Type-2 and I put it into use there.

I'm looking to verify that there's no way to get around this. so, I cannot somehow put an int array on a[0], a char array on a[1]-- Java arrays are single-type.

I'm aware that I can declare 2 parallel arrays-- one int[] and one char[] and that solves it.

Thanks in advance.

//=====================

Edit: The good old object class solves it-- as the useful answers below pointed out. Thank you for your input.

Upvotes: 1

Views: 1667

Answers (2)

AlexWien
AlexWien

Reputation: 28727

You can use an array of Objects:

Object[] x = new Object[2];
x[0] = new Integer[3];
x[1] = new String[3];

Upvotes: 4

Juvanis
Juvanis

Reputation: 25950

You can use Object[].

Object[] arrays = new Object[2];
arrays[0] = new int[10];
arrays[1] = new char[10];

Upvotes: 5

Related Questions