I have an array and want to make it generic. Its size is 10 and I want it to either contain int values or string values. Array type depends on the user's selection at runtime.
Is this possible?
- PEACE
Printable View
I have an array and want to make it generic. Its size is 10 and I want it to either contain int values or string values. Array type depends on the user's selection at runtime.
Is this possible?
- PEACE
You can create an object array. If you use the auto boxing feature you can just put any object in the array.
Is this help you?Code:Object[] data = new Object[2];
data[0] = 10;
data[1] = "hello";
or u can use the arraylist man..then u can put any type of variable or any type of object into that oone thats useful 2 u i think..
public class Testing
{
public static void main(String args[])
{
Integer i1=new Integer(45);//Integer object
Double d1=new Double(45.567);//double object
String s1=new String("agsd");//string object
Calc c1=new Calc();//object of the class
ArrayList a1= new ArrayList();
a1.add("45");// u can insert direct values also like this
a1.add("a");
a1.add("4de5");
a1.add(45);
a1.add(45.56);
a1.add('a');
a1.add('4');
a1.add("45.79");
a1.add(i1);a1.add(d1);a1.add(s1);
a1.add(c1);
System.out.println(""+a1);
}
}
So I can either use ArrayList or use an object array. Thanks for the details.
- PEACE