1

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.

1
  • 1
    Why are collections not fitting? You could use a something like List<Object, List<Object>> Commented Feb 5, 2014 at 21:01

2 Answers 2

5

You can use Object[].

Object[] arrays = new Object[2];
arrays[0] = new int[10];
arrays[1] = new char[10];
Sign up to request clarification or add additional context in comments.

Comments

4

You can use an array of Objects:

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

3 Comments

@Roam P.S. my answer was the first.
Yes first, but it did not show the usage of different types, which you added later.
well- can ttell by the secs. one of oyu got the upvote, the other the accept. trying to be fair to e1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.