11

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?

eugenhu
  • 1,168
  • 13
  • 22
Soni Sori
  • 123
  • 1
  • 1
  • 5

4 Answers4

9

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.

Nick
  • 368
  • 1
  • 7
  • 16
  • ok.Thank You. Now I have a code in Python.There is a list called [open].There are three variable g,x,y. Now I define a command like that in Python is open=[[g,x,y]]. If I want to implement it in Java I try to implement it like that.ArrayListlist1=new ArrayList(); ArrayListopen=new ArrayList(); open.add(g);open.add(x);open.add(y); list1.add(open);But whenever I used a forloop and the value of g,x,y changed it added in the open list altogether not list wish like open=[[g,x,y,g1,x1,y1,g2,x2,y2]] but I want to add it like open=[[g,x,y][g1,x1,y1][g2,x2,y2]]. – Soni Sori Feb 13 '18 at 23:05
  • If i understand correctly you want to add a new arraylist in list1? Something like this : for (int i = 0; i < 3; i++) { ArrayList open = new ArrayList<>(); g += i; x += i; y += i; open.add(g); open.add(x); open.add(y); list1.add(open); } – Nick Feb 14 '18 at 02:43
  • The answer is [[0, 0, 0]] [[0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1]] [[0, 0, 0, 1, 1, 1, 3, 3, 3], [0, 0, 0, 1, 1, 1, 3, 3, 3], [0, 0, 0, 1, 1, 1, 3, 3, 3]] There is the main problem lies.I initialize x,y,z firstly 0,0,0 so at first it gave a output like that [[0,0,0]].But for the 2nd time when the loop is iterate and the values will be incremented by 1 it give output [[0,0,0,1,1,1]] but I need separated output for every iteration. Like [[0,0,0],[1,1,1]] instead of [[0,0,0,1,1,1]] – Soni Sori Feb 14 '18 at 16:32
  • oh I slove this problem..Thanks any way. Now can you tell me how to sort a Arraylist type list in Java? – Soni Sori Feb 14 '18 at 17:21
  • I'm glad to hear you fixed your problem. Please keep in mind that for follow up questions it's better to either start a new question or do some research before. To sort a a list of integers you can use Collections.sort(arrayList); To sort a list of objects you wanna use either Comparable or Comparator. You can read about them here https://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/ – Nick Feb 14 '18 at 17:44
6

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?

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().

Parker
  • 71
  • 1
  • 1
  • 7
0

Take a look at Collections in java. There are many lists (ArrayList, LinkedList etc). Choose the best datastructure needed for the requirement and complexity (both space and time).

Guru
  • 1,653
  • 1
  • 7
  • 14