tags:

views:

171

answers:

6

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.

+12  A: 
playersList.get(0)

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

Matthew Flaschen
+6  A: 

You have to access lists a little differently than arrays in Java. See the javadocs for the List interface for more information.

playersList.get(0)

However if you want to find the smallest element in playersList, you shouldn't sort it and then get the first element. This runs very slowly compared to just searching once through the list to find the smallest element.

For example:

int smallestIndex = 0;
for (int i = 1; i < playersList.size(); i++) {
    if (playersList.get(i) < playersList.get(smallestIndex))
        smallestIndex = i;
}

playersList.get(smallestIndex);

The above code will find the smallest element in O(n) instead of O(n log n) time.

jjnguy
I wander why such a basic operation should be programed. Why Java does not provide a function which returns just a minimal value from array?
Roman
Roman, there is such a method, Collections.min (http://java.sun.com/javase/7/docs/api/java/util/Collections.html#min%28java.util.Collection%29).
Matthew Flaschen
+1  A: 

That depends on what type your list is, for ArrayList use:

list.get(0);

for LinkedList use:

list.getFirst();

if you like the array approach:

list.toArray()[0];
rsp
bad advice. LinkedList implements the List interface, no need to use a special method (and I'd be amazed of there was any performance difference between the two). And calling toArray() is wasteful--you might be allocating the list into a new array for no reason!
Kip
@Kip, strange remark; the LinkedList class does not implement the first and last methods out of spite. If you have a good reason to use a LinkedList, you should not refrain from using its methods just because they are not in the List interface. The array example can be usefull if the list itself is not needed after sorting, and closest to what OP asked. Without knowing the context of the source code in question you cannot determine the validity of the advice.
rsp
@rsp: why would it matter whether or not you need the list after sorting? in either case (or even if the list is never sorted) calling toArray() just to get the first element (may) needlessly create an entire array.
Kip
@Kip, you are right in that retrieving an array just for the first element is ot a good idea if you don not plan to use the array. The question asked for how to get the first element from a list and mentioned an array context. I merely gave alternatives touching those concepts. It is up to the programmer who knows his source to make a choice from these alternatives.
rsp
+1  A: 

Matthew's answer is correct:

list.get(0);

To do what you tried:

list[0];

you'll have to wait until Java 7 is released:

devoxx conference

Here's an interesting presentation by Mark Reinhold about Java 7

It looks like parleys site is currently down, try later :(

OscarRyz
A: 

If your collection is not a List (and thus you can't use get(int index)), then you can use the iterator:

Iterator iter = collection.iterator();
if (iter.hasNext()) {
    Object first = iter.next();
}
Steve Kuo
A: 

If you just want to get the minimum of a list, instead of sorting it and then getting the first element (O(N log N)), you can use do it in linear time using min:

<T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)

That looks gnarly at first, but looking at your previous questions, you have a List<String>. In short: min works on it.

For the long answer: all that super and extends stuff in the generic type constraints is what Josh Bloch calls the PECS principle (usually presented next to a picture of Arnold -- I'M NOT KIDDING!)

Producer Extends, Consumer Super

It essentially makes generics more powerful, since the constraints are more flexible while still preserving type safety (see: what is the difference between ‘super’ and ‘extends’ in Java Generics)

polygenelubricants