# Collections.binarySearch



## comp89 (5. Apr 2005)

hey
beispiel vom heutigen programmiertest


```
import java.util.ArrayList;
import java.util.Collections;

public class Test {

    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        System.out.println(Collections.binarySearch(list, "4"));
        System.out.println(Collections.binarySearch(list, "6"));
        System.out.println(Collections.binarySearch(list, "1"));
    }
}
```

gefragt war die ausgabe

gleich ausprobiert nachdem ich heim kam:

Ausgabe


> 3
> -6
> 0



und warum -6?


----------



## Roar (5. Apr 2005)

lies doch api: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#binarySearch(java.util.List,%20T)


> returns:
> index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(),


----------

