Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
How were types handled without generics in ArrayList before java version 1.5

I have a question about the history of Java.
Java has had ArrayList since 1.2.
Java has generics since version 1.5.

How was the implementation of ArrayList without generics to define the type?

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

It was all done with Object (and there was a lot of casting involved), e.g.:

ArrayList list = new ArrayList();
list.add(new Thingy());
// ...
Thingy t = (Thingy)list.get(0);
// Note ---^^^^^^^^

The list only knew what it stored was Object, it was up to the code using the list to cast back to a useful type.

As you can imagine, this lead to all sorts of unpleasantness — you could put the wrong kind of object in the list, and then get ClassCastExceptions later when you tried to cast it to the type you were expecting; if you changed what was in the list, you had to change your casts everywhere and inevitably forget one, etc., etc. Generics helped remove those pain points.

about 4 years ago · Santiago Trujillo Denunciar

0

Before generics, ArrayList have the Object as a type so that you can insert and get back the type Object which mean that you need to cast them while retrieving back to respected type. After generics these kind of redundant code got removed.

If you have close look at generic docs, your specific question been answered

Elimination of casts.

The following code snippet without generics requires casting:
List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0);

When re-written to use generics, the code does not require casting:

    List<String> list = new ArrayList<String>();
    list.add("hello");
    String s = list.get(0);   // no cast
about 4 years ago · Santiago Trujillo Denunciar

0

Actually you should not ask these type of questions in stackoverflow.What everyone expects is somecode from you to debug..But anyway you seems to be like new to stackoverflow...

You can use arraylist without generics too. For example if you use generics to hold items of particular type it looks like this..

ArrayList<String> list=new ArrayList<>();

If you don't want to use generics you can simply use this

ArrayList list =new ArrayList();

Let me tell you the very big disadvantage of using without generics.If you don't use generics then it assumes every thing as objects.So you need to type cast these to your particular datatype each time you retreive elements.

For example

for (Object o:list)
{
String s=(String)o;
System.out.println(s);
}

is what you have to do if you dont use generics.. If you use generics

then

for(String s:list)
{
System.out.println(s)
}

Hope this helps....:)

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda