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

473
Visualizações
Java equivalent of Python List

In Python there is a data structure called 'List'. By using 'List' data structure in Python we can append, extend, insert, remove, pop, index, count, sort, reverse.

Is there any similar data structure in Java where we can get all that function like Python List?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The closest Java has to a Python List is the ArrayList<> and can be declared as such

//Declaring an ArrayList
ArrayList<String> stringArrayList = new ArrayList<String>();

//add to the end of the list
stringArrayList.add("foo");

//add to the beggining of the list
stringArrayList.add(0, "food");

//remove an element at a spesific index
stringArrayList.remove(4);

//get the size of the list
stringArrayList.size();

//clear the whole list
stringArrayList.clear();

//copy to a new ArrayList
ArrayList<String> myNewArrayList = new ArrayList<>(oldArrayList);

//to reverse
Collections.reverse(stringArrayList);

//something that could work as "pop" could be
stringArrayList.remove(stringArrayList.size() - 1);

Java offers a great selection of Collections, you can have a look at a tutorial that Oracle has on their site here https://docs.oracle.com/javase/tutorial/collections/

IMPORTANT: Unlike in Python, in Java you must declare the data type that your list will be using when you instatiate it.

over 4 years ago · Santiago Trujillo Relatório

0

Several collections exist, but your probably looking for ArrayList

In Python you can simply declare a list like so:

myList = []

and begin using it.

In Java, it better to declare from the interface first so:

List<String> myList = new ArrayList<String>();

Python          Java
append          add
Remove          remove
len(listname)   list.size

Sorting a List can require a little more work, for example, depending on the objects you may need to implement Compactor or Comparable.

ArrayList will grow as you add items, no need to extend it on your own.

As for reverse() and pop(), I'll refer you can refer to:

How to reverse a list in Java?

How to pop items from a collection in Java?

over 4 years ago · Santiago Trujillo Relatório

0

Java has an interface called list, which has implementations such as ArrayList, AbstractList, AttributeList, etc.

https://docs.oracle.com/javase/8/docs/api/java/util/List.html

However, each one has different functionalities, and I don't know if they have everything you've specified such as .reverse().

over 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