Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a first element from a sorted list?

Tags:

java

list

sorting

I used Collections.sort(playersList); to sort a List. So, I think playersList is sorted now. But how can I get the first element of the list? playersList[0] does not work.

like image 339
Roman Avatar asked Mar 23 '10 20:03

Roman


People also ask

How do I get the first item in a list?

Get the first element of ArrayList with use of get(index) method by passing index = 0. Get the last element of ArrayList with use of get(index) method by passing index = size – 1.

How do you sort first element in Python?

In Python, we can use the sort() method to sort the elements in the ascending order, and by default, it will sort the first element.

Which method is used to return first occurrence of given value in a list?

To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found. The following illustrates the syntax of the indexOf() method.

How do you sort a list of lists by first element and then by second element in Python?

Use the sort() Function to Sort a List of Lists in Python. The sort() method sorts the list of lists in Python according to the first element of each inner list. This method makes changes in the original list itself. We use the reverse parameter to sort in descending order.


1 Answers

playersList.get(0) 

Java has limited operator polymorphism. So you use the get() method on List objects, not the array index operator ([])

like image 180
Matthew Flaschen Avatar answered Sep 23 '22 01:09

Matthew Flaschen