OnJava8-Examples/arrays/SimpleSetAll.java

58 lines
1.5 KiB
Java
Raw Normal View History

2016-01-25 18:05:55 -08:00
// arrays/SimpleSetAll.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 static onjava.ArrayShow.*;
class Bob {
final int id;
2017-05-01 14:33:10 -06:00
Bob(int n) { id = n; }
@Override public String toString() {
return "Bob" + id;
}
2016-01-25 18:05:55 -08:00
}
public class SimpleSetAll {
public static final int SZ = 8;
2017-05-01 17:43:21 -06:00
static int val = 1;
2016-01-25 18:05:55 -08:00
static char[] chars = "abcdefghijklmnopqrstuvwxyz"
.toCharArray();
static char getChar(int n) { return chars[n]; }
public static void main(String[] args) {
int[] ia = new int[SZ];
long[] la = new long[SZ];
double[] da = new double[SZ];
Arrays.setAll(ia, n -> n); // [1]
2016-01-25 18:05:55 -08:00
Arrays.setAll(la, n -> n);
Arrays.setAll(da, n -> n);
show(ia);
show(la);
show(da);
Arrays.setAll(ia, n -> val++); // [2]
2016-01-25 18:05:55 -08:00
Arrays.setAll(la, n -> val++);
Arrays.setAll(da, n -> val++);
show(ia);
show(la);
show(da);
Bob[] ba = new Bob[SZ];
Arrays.setAll(ba, Bob::new); // [3]
2016-01-25 18:05:55 -08:00
show(ba);
Character[] ca = new Character[SZ];
Arrays.setAll(ca, SimpleSetAll::getChar); // [4]
2016-01-25 18:05:55 -08:00
show(ca);
}
}
/* Output:
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
[1, 2, 3, 4, 5, 6, 7, 8]
[9, 10, 11, 12, 13, 14, 15, 16]
[17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0]
[Bob0, Bob1, Bob2, Bob3, Bob4, Bob5, Bob6, Bob7]
[a, b, c, d, e, f, g, h]
*/