39

I am working with arrays in Java and I have got a question. I know that an array in Java is a collection of similar data types, as shown below:

int[] x = new int[]{1,2,3};

The above declaration can be read as an Integer array which is a collection of integer types.

Consider this:

Object[] x = new Object[]{1,2,3,"srk"};

Here, can I say that the above is an array which is a collection of dis-similar data types, or is it an Object array of similar data types, i.e. objects?

I am muddled and skeptical about this. In Java, is it possible to create an array or any sort of collection which can hold different data types?

David Walschots
  • 12,279
  • 5
  • 36
  • 59
srk
  • 4,857
  • 12
  • 65
  • 109
  • 3
    @duffymo, Treating the JDK as an expert only works for things on which most JDK implementations are likely to agree and won't reliably suss out strange cases like array variance in Java. – Mike Samuel May 03 '13 at 16:43
  • For something like this, which has been possible since JDK 1.0, I'd disagree. And all the more reason to suss out strange cases for your JDK. Are you suggesting that "expert" advice here will suss them out more efficiently? – duffymo May 03 '13 at 17:35
  • @duffymo, I assert that questions of the form "is this idiomatic in language X", "does this have pitfalls", and "is this speced or peculiar to my test environment" should be tested and put to experts. I assert that heterogeneous arrays touch on all three of those forms, since different languages have different alternative approaches (Haskell) or idiomatic preference (Ruby vs C void*) so it is reasonable to wonder what is idiomatic in Java; pitfalls are precisely what you are unlikely to find until you trip over them (etymology of "pitfall"); many languages have undefined behavior. – Mike Samuel May 03 '13 at 18:52
  • 2
    why is this question closed ? – humble_wolf Oct 20 '21 at 05:58

6 Answers6

50

ALL objects in Java extend Object.

Therefore it is possible to be completely non-descriptive when you create the array by declaring it an array of Objects:

Object[] arr = new Object[6];

This code creates an array of objects of length 6.

So for instance, you could create an array where entries come in pairs of two. In this case, the first object is a String and the second is an Integer.

Object[] arr = new Object[6];

arr[0] = new String("First Pair");
arr[1] = new Integer(1);
arr[2] = new String("Second Pair");
arr[3] = new Integer(2);
arr[4] = new String("Third Pair");
arr[5] = new Integer(3);

Now if you want to actually figure out what these objects are then it will require a cast:

int x = (Integer)arr[1];
Perplexing Pies
  • 123
  • 1
  • 1
  • 9
M Sach
  • 33,416
  • 76
  • 221
  • 314
7

To add to the other answers, you can put whatever you want in an array of Objects. But if you wish to access any of methods or properties, not shared with Object, that a specific element has, then you have to down-cast it to the needed type as Java will recognise it as type Object - this is something you have to be careful with.

Example:

    Object test[];
    test = new Object[]{1, 2, "three", new Date()};
    System.out.println( ( (Date)test[3] ).getMonth() );
    // the above line will output '4', but there will be a compilation error
    // if the cast (Date) is emitted
Griffin
  • 13,184
  • 4
  • 29
  • 43
  • 1
    This works for me. But, I am still wondering whether it is possible to have an array inside an array something like - `[1, 2, "Sid", 45, [1, 6, "Saha"], "SO"]` – Siddharth Jan 23 '19 at 04:28
4

It works exactly how you thought:

Object[] x = new Object[]{1,2,3,"srk"};
for(Object o: x){
   System.out.println(o.getClass());
}

Output:

class java.lang.Integer
class java.lang.Integer
class java.lang.Integer
class java.lang.String
Kuchi
  • 4,204
  • 3
  • 29
  • 37
2

In java, is it possible to create an array or any sort of collection which can hold different data types?

Yes.

"Heterogeneous collection" is the term most often used for this, and
what is the point of heterogenous arrays? discusses them in java.

For collection types, you can use List<Object> to which you can add many kinds of objects, and List<?> which can receive many kinds of lists but which you cannot add to because of type variance.


The variance of Java arrays is a little odd though because

Object[] arr = new String[3];  // OK
List<Object> list = new ArrayList<String>();  // DOES NOT COMPILE

arr[0] = Integer.valueOf(42);  // RUNTIME ERROR

so when you see an Object[] you need to know that it was created via new Object[] for it to be safe to use as a heterogenous array. This is unlike a Collection<Object> where the type-system gives you some degree of safety.

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
0

In Java you can create an array of Objects

Object[] x = new Object[10];

and you can assign references to instances of any class to its elements since any class in Java is an Object.

But it is different with primitive arrays. int[] can contain only int elements, long[] only longs, etc

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

Creating an Object[] array is one way to do it. Otherwise, you can create a class with the variables you want, and have an array of that class's objects.

class MyClass{
    int var_1;
    String var_2;
    ...
    ...
}
...
MyClass[] arr = new MyClass[3];

Then, use arr where you want to.

Varad
  • 155
  • 1
  • 10