Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use generics in lists in Java 1.4?

I have to use Java 1.4 and am using arraylist structures. Now I need to do some re-factoring and it would help if I can use Generics. Currently I have code like this

ArrayList rows = new ArrayList();

Which is a straightforward ArrayList. But now the "rows" variable is becoming more complex and need to define a "Row" class. I now want to do something like what you see in J2SE 1.5

ArrayList<Row> rows = new ArrayList();

A trawl through Google doesn't expose any answers without some sophisticated use of 3rd party solutions. How would my code have to change to accommodate this in 1.4, without using 3rd party solutions/open source projects, (if possible)?

like image 551
angryITguy Avatar asked Jun 12 '11 05:06

angryITguy


1 Answers

Generics were introduced in JDK 1.5. So you can't use them in 1.4. However, you may be able to use JDK 1.5 compiler but target 1.4 with your classes with -target 1.4 javac option while keeping -source 1.5 option to indicate that your source is 1.5 compatible. In this case you can use generics as they should not affect result classes.

See the section on Cross-Compilation Options

like image 145
Alex Gitelman Avatar answered Sep 28 '22 04:09

Alex Gitelman