Update 'README.md'

This commit is contained in:
2022-08-15 16:18:50 +00:00
parent e0a045f1e8
commit 078c0273bf

View File

@@ -32,21 +32,25 @@ dependencies {
# Examples
## IntList / LongList
An alternative to `int[]`/`long[]` that grows dynamically. The list supports a few features that are either not supported
by arrays or that would be expensive to implement. The most unique feature is that the list knows if it is sorted.
### isSorted()
The lists keep track of whether they are sorted or not. This makes the call to `isSorted()` very cheap, because we only have to return the value of a field. The drawback is that some operations are much more expensive. This affects `shuffle()`,
as well as `remove(int, int)`, `removeIf()`, `removeAll(IntList)` and 'retainAll()' if the list was not sorted before.
The fact that we know if a list is sorted allows us to use more efficient algorithms for `union(IntList, IntList)`, `intersection(IntList, IntList)`, `indexOf(int)`, `lastIndexOf()` and `uniq()`.
```java
import org.lucares.collections.IntList;
final IntList list = IntList.of(1, 3, 5);
System.out.println(list + " is sorted: " + list.isSorted());
public class Example {
public static void main(final String[] args) {
final IntList list = IntList.of(1, 3, 5);
System.out.println(list + " is sorted: " + list.isSorted());
list.insert(2, 7);
System.out.println(list + " is sorted: " + list.isSorted());
list.insert(2, 7);
System.out.println(list + " is sorted: " + list.isSorted());
list.sort();
System.out.println(list + " is sorted: " + list.isSorted());
}
}
list.sort();
System.out.println(list + " is sorted: " + list.isSorted());
```
Running this program gives the following output:
@@ -56,3 +60,9 @@ Running this program gives the following output:
[1, 3, 7, 5] is sorted: false
[1, 3, 5, 7] is sorted: true
```
The fact that we know if a list is sorted allows us to use more efficient algorithms for `union(IntList, IntList)`, `intersection(IntList, IntList)`, `indexOf(int)`, `lastIndexOf()` and `uniq()`.