OnJava8-Examples/arrays/CountUpward.java

25 lines
708 B
Java
Raw Permalink Normal View History

2016-01-25 18:05:55 -08:00
// arrays/CountUpward.java
// (c)2021 MindView LLC: see Copyright.txt
2016-01-25 18:05:55 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
import java.util.*;
import java.util.stream.*;
import static onjava.ArrayShow.*;
public class CountUpward {
static long[] fillCounted(int size) {
return LongStream.iterate(0, i -> i + 1)
.limit(size).toArray();
}
public static void main(String[] args) {
long[] l1 = fillCounted(20); // No problem
show(l1);
// On my machine, this runs out of heap space:
//- long[] l2 = fillCounted(10_000_000);
}
}
/* Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19]
2016-01-25 18:05:55 -08:00
*/