Multiple generic arrays
is it possible to create something like <String, Integer>[] a;
If I wanted to create an array that stores objects of String and Integer?

Recommended Answers

All 2 Replies

I don't know of any way (others may know better?), but I'm curious as to why you want to do that. You will still need to test and cast anything you get from the array because String's methods and Integer's methods are so different.
Depending on why you are doing it, it may be a good representation to have a wrapper class for your data, like

class MyDataStringOrInt {
   private String s; private int i;
   public MyDataStringOrInt(String stringValue) { ...
   public MyDataStringOrInt(int intValue) { ...
   public boolean isString() { ...
   public boolean isInt() { ...
   public String getStringValue() { ...

yes and no.
Creating something like

<E extends Object>[] arr;

might work (I've not tested this, not interested), but isn't really useful as you might as well write

Object[] arr;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.