Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

190
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda