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?
You have to use the object array when you want to create an array of multiple data types in C#. By declaring the array as an Object you can have multiple data types. object[] requiredArray = new object[5];As System.
Yes we can store different/mixed types in a single array by using following two methods: Method 1: using Object array because all types in . net inherit from object type Ex: object[] array=new object[2];array[0]=102;array[1]="csharp";Method 2: Alternatively we can use ArrayList class present in System.
An Array of Objects is created using the Object class, and we know Object class is the root class of all Classes. We use the Class_Name followed by a square bracket [] then object reference name to create an Array of Objects.
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];
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];
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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With