Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare an array of different data types [closed]

Tags:

java

arrays

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?

like image 928
srk Avatar asked May 03 '13 16:05

srk


People also ask

How do I create an array of 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.

Can an array hold different data types?

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.

How do you declare an array which can hold any type of objects?

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.

How do you create an array of different types in java?

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];


4 Answers

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];
like image 159
M Sach Avatar answered Oct 18 '22 00:10

M Sach


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
like image 7
Griffin Avatar answered Oct 18 '22 02:10

Griffin


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
like image 4
Kuchi Avatar answered Oct 18 '22 00:10

Kuchi


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.

like image 2
Mike Samuel Avatar answered Oct 18 '22 01:10

Mike Samuel