0

I need a class which can store and return values of differnt types. I couldn't define my own class. The only class I found that meets my needs is List class. I retrieve the values from database Table. This is my approach:

//the total number of these variables can vary. The same is true for their types;
int intValue = 1;
String stringValue = "text 1";
byte[] dataValue = new byte[4];


List row = new ArrayList();
row.add(intValue);
row.add(stringValue);
row.add(dataValue);

List rows = new ArrayList();
rows.add(row);

intValue = 6;
stringValue = "text 2";
dataValue = new byte[8];

row = new ArrayList();
row.add(intValue);
row.add(stringValue);
row.add(dataValue);

rows.add(row);

String text1 = (String) ((List)rows.get(0)).get(1);
String text2 = (String) ((List)rows.get(1)).get(1);

Can you suggest a better way to store generic type data because I think my implementation is wrong.

vovahost
  • 34,185
  • 17
  • 113
  • 116
  • 1
    "Can you suggest a better way to store generic type" - The answer is in your question. – Maroun Nov 16 '14 at 15:32
  • 2
    Storing variables of type `Object` in the same data structure is not genericity and is most of the time bad design. – Dici Nov 16 '14 at 15:33
  • But what about the casting, is it resource consuming ? – vovahost Nov 16 '14 at 15:35
  • 1
    @vovahost Yes, casting has a runtime cost, that is one reason why this is bad design http://stackoverflow.com/questions/2170872/does-java-casting-introduce-overhead-why – Dici Nov 16 '14 at 15:45

2 Answers2

1

Well... You can make the class generic, parametrized by the type you want to use. Or you could maybe have T1 t1s[]; T2 t2s[]; T3 t3s[] in your code and just leave variables for each piece of data, with the understanding that only one is active at a time.

But do you really mean data? Because data should be a string or JSON or XML or something. In which case just store that and pass it.

But do you really mean you want an indefinite number of objects of arbitrary data type? Well, how on earth would you code with that? You get back an object that could be of any type, so you can't call any specific methods on it. You don't know what the type is since it's fully dynamic so you can't even extract with a bunch of instanceof statements.

Or maybe all of your data has a common base class and you only need base class methods? Perfect, extend a common base class. This is what inheritance is for.

tl;dr: your design is bad and Java is designed to prohibit it. Fix it.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • By data I mean different values. I retrieve values from a database Table. Tables can have different Columns types like String, int. My listOfObjects is a row from database. – vovahost Nov 16 '14 at 15:54
  • I know the type of every Object or primitive because I have the list of Columns and their data types – vovahost Nov 16 '14 at 16:00
1

I would personally choose an ArrayList because it is dynamic but you can do the same exact thing with an array. Are you saying that you cannot figure out how to define a class that will hold a generic type? All you have to do is define it as a collection that holds any object/generic type:

ArrayList<Object> myList = new ArrayList<>();
myList.add(new String("Some String"));
myList.add(2);
System.out.println(myList.get(0));
System.out.println(myList.get(1));

You really can add whatever type of object you'd like in "myList". That is the main benefit of using polymorphism in Java.

StreamingBits
  • 137
  • 11