Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Array with multiple data types

Tags:

java

What can I use to store multiple different types of data, Int/String/etc.? I come from a PHP background where I can store different types of data into an array, but I don't know how to do that in Java.

Take this example:

$array = array(
    "val1" => 1,
    "val2" => "cat",
    "val3" => true
);

How can I make something similar in Java?

like image 878
Get Off My Lawn Avatar asked May 30 '14 14:05

Get Off My Lawn


People also ask

Can an array contains multiple data types in Java?

No, we cannot store multiple datatype in an Array, we can store similar datatype only in an Array.

Can we store different data types in ArrayList Java?

ArrayList is a kind of List and List implements Collection interface. The Collection container expects only Objects data types and all the operations done in Collections, like iterations, can be performed only on Objects and not Primitive data types. An ArrayList cannot store ints.

Can a multidimensional array have different data types?

You can have multiple datatypes; String, double, int, and other object types within a single element of the arrray, ie objArray[0] can contain as many different data types as you need. Using a 2-D array has absolutely no affect on the output, but how the data is allocated.

Can stack in Java have multiple data types?

If you are to implement the stack with arrays, then within the stack array you need to maintain a union so that you can store different data types. Along with this you need to have stack top variable and an array to keep track of data type of each array position.


2 Answers

Java is a strongly typed language. In PHP or Javascript, variables don't have a strict type. However, in Java, every object and primative has a strict type. You can store mutliple types of data in an Array, but you can only get it back as an Object.

You can have an array of Objects:

Object[] objects = new Object[3];
objects[0] = "foo";
objects[1] = 5;

Note that 5 is autoboxed into new Integer(5) which is an object wrapper around the integer 5.

However, if you want to get data out of the array, you can only get it as an Object. The following won't work:

int i1 = objects[1]; // Won't work.
Integer i2 = objects[2]; // Also won't work.

You have to get it back as an Object:

Object o = objects[0]; // Will work.

However, now you can't get back the original form. You could try a dangerous cast:

String s = (String) o;

However you don't know that o is a String.

You can check with instanceof:

String s = null;

if (o instanceof String)
    s = (String) o;
like image 197
Anubian Noob Avatar answered Oct 03 '22 20:10

Anubian Noob


You could use an object array but that creates problems when the time comes to retrieve the objects you have stored. Instead I would use a typesafe heterogenous container as described in Effective Java (and linked to earlier in this sentence).

public class DateStuff{

    private Map<Class<?>, Object> dateMap =
        new HashMap<Class<?>, Object>();

    public <T> void putDate(Class<T> type, T instance){
          if(type == null)
              throw new NullPointerException("Type null");
          dateMap.put(type, instance);
    } 

    public<T> getDate(Class<T> type){
          return type.cast(dateMap.get(type));
    }

}

The typesafe heterogenous container solves the problem of retrieving objects later by mapping objects by their class. In your case I would combine this with other data structures - for example List<Date>, List<String>, or List<Integer>, as the base classes to provide a way to store multiple different kinds of objects in one collection. Then to retrieve values you would simply get the sub collection, e.g. a List<Date>, knowing that all items contained therein were of the same class.

like image 38
Rarw Avatar answered Oct 03 '22 21:10

Rarw