Question:
can you have a mixed data type array in java?
Simon
2009-06-11 04:32:27 UTC
I know java does not let you use different data types in one array. How could I represent the following in a two dimensional array in java? (this would be easy in vb but need to use java).
thanks

dimension1 dimension2
array ["A"] [1]
array ["B"] [2]
array ["C"] [3]
array ["D"] [4]
Five answers:
Mike S
2009-06-11 05:14:55 UTC
It is not replacement but you might try using hashmap or Hashtable.

e.g

Hashtable ar=new Hashtable();

ar.put("A",new String[1]);

ar.put("B",new String[2]);...
Blackcompe
2009-06-11 12:31:47 UTC
If you add an Object to a String array, a compile time error is thrown. If no error was thrown you'd have ClassCastExceptions occurring all the time. The only other alternative is to create a 2-D array of Objects. Object is a super class of all classes in Java; it serves as a polymorphic handle for any instance of a Java class. For instance, you have:



Object[][] ar = new Object[2][2];

ar[0][0] = new String("A"); ar[0][1] = new Integer(1);

ar[1][0] = new Double(2); ar[1][1] = new Character('b');



Although, each element of this array contains an instance of a different class, we can guarantee that it is a subclass of Object. Therefore, we handle the object with a reference of type Object. This is polymorphism at work. Here's another example:



class Animal{}

class Dog{}

class Cat{}

Animal[] pound = new Animal[3];

pound[0] = new Animal();

pound[1] = new Dog();

pound[2] = new Cat();



When we access an element of the array, we must handle it with a reference to Animal. We can try downcasting it to a Dog or Cat, but if we're wrong, a ClassCastException will be thrown. It's the same with the first example. We handle all the elements with a reference to Object, then we try downcasting, but we must be right, or a ClassCastException is thrown.



Object obj = ar[0][0];

String s = (String)obj; //OK

Double d = (Double)obj; //ClassCastException will be thrown



So, here the first element, which is unknown at runtime, is a String. We only handle it as an Object, because we have to. We try downcasting to String, it works. Then, we try downcasting to a Double; but, it's a String, so no go.



I hope I explained this clearly. So, an Object array is the only solution, but you (the programmer) must know the object's type, and that's a pain.
deonejuan
2009-06-14 18:03:29 UTC
If you don't use Object as the type, then you have to use abstract as your base of your hierarchy, subclassing to the behaviors you need.



abstract Fish

class Freshwater extends Fish

class Saltwater extends Fish

class Porpoise extends Saltwater

class Trout extends Freshwater



Fish[] fish = new Fish[10];

fish[0] = new Trout();

fish[1] = new Porpoise();



if( fish[ x ] instanceof Porpoise )

...



that's for a mixed data type, but I don't see your problem if you approached this algo as two parallel arrays, a hashtable, linked list or enumeration solution. My approach would be parallel arrays from what you show here.
anonymous
2009-06-11 21:34:38 UTC
What seems like the most straightforward way to do this is use a map. You want to have ("A",1) be an "index" into some kind of structure, so we can just do that (in this case by coercing your indices into strings:



http://gist.github.com/128250
anonymous
2009-06-11 11:36:55 UTC
No, you can't.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...