OnJava8-Examples/arrays/CollectionComparison.java

53 lines
1.4 KiB
Java
Raw Permalink Normal View History

2015-12-15 11:47:04 -08:00
// arrays/CollectionComparison.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -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.
2015-06-15 17:47:35 -07:00
import java.util.*;
2016-01-25 18:05:55 -08:00
import onjava.*;
import static onjava.ArrayShow.*;
2015-06-15 17:47:35 -07:00
class BerylliumSphere {
private static long counter;
private final long id = counter++;
@Override public String toString() {
2017-01-20 21:30:44 -08:00
return "Sphere " + id;
}
2015-06-15 17:47:35 -07:00
}
2015-12-15 11:47:04 -08:00
public class CollectionComparison {
2015-06-15 17:47:35 -07:00
public static void main(String[] args) {
2017-01-20 21:30:44 -08:00
BerylliumSphere[] spheres =
new BerylliumSphere[10];
2015-06-15 17:47:35 -07:00
for(int i = 0; i < 5; i++)
spheres[i] = new BerylliumSphere();
2016-01-25 18:05:55 -08:00
show(spheres);
2015-11-03 12:00:44 -08:00
System.out.println(spheres[4]);
2015-06-15 17:47:35 -07:00
2016-01-25 18:05:55 -08:00
List<BerylliumSphere> sphereList = Suppliers.create(
ArrayList::new, BerylliumSphere::new, 5);
2015-11-03 12:00:44 -08:00
System.out.println(sphereList);
System.out.println(sphereList.get(4));
2015-06-15 17:47:35 -07:00
int[] integers = { 0, 1, 2, 3, 4, 5 };
2016-01-25 18:05:55 -08:00
show(integers);
2015-11-03 12:00:44 -08:00
System.out.println(integers[4]);
2015-06-15 17:47:35 -07:00
List<Integer> intList = new ArrayList<>(
Arrays.asList(0, 1, 2, 3, 4, 5));
intList.add(97);
2015-11-03 12:00:44 -08:00
System.out.println(intList);
System.out.println(intList.get(4));
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
[Sphere 0, Sphere 1, Sphere 2, Sphere 3, Sphere 4,
null, null, null, null, null]
2015-06-15 17:47:35 -07:00
Sphere 4
[Sphere 5, Sphere 6, Sphere 7, Sphere 8, Sphere 9]
Sphere 9
[0, 1, 2, 3, 4, 5]
4
[0, 1, 2, 3, 4, 5, 97]
4
2015-09-07 11:44:36 -06:00
*/