Skip to content

Commit 3cfc0a3

Browse files
committed
2025-01-15 last push
1 parent 731c767 commit 3cfc0a3

8 files changed

Lines changed: 25 additions & 6 deletions

File tree

‎arrays/ArrayCopying.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public static void main(String[] args) {
5151
Arrays.copyOf(d, d.length, Sup[].class); // [4]
5252
show(b);
5353

54+
// Arrays.setAll(b, Sup::new);
55+
5456
// This "downcast" works fine:
5557
Sub[] d2 =
5658
Arrays.copyOf(b, b.length, Sub[].class); // [5]

‎arrays/ArrayOfGenericType.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public class ArrayOfGenericType<T> {
88
@SuppressWarnings("unchecked")
99
public ArrayOfGenericType(int size) {
1010
// error: generic array creation:
11-
//- array = new T[size];
11+
// array = new T[size];
1212
array = (T[])new Object[size]; // unchecked cast
1313
}
1414
// error: generic array creation:
15-
//- public <U> U[] makeArray() { return new U[10]; }
15+
// public <U> U[] makeArray() { return new U[10]; }
1616
}

‎arrays/ArrayOfGenerics.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,28 @@ public static void main(String[] args) {
1212
ls = (List<String>[])la; // Unchecked cast
1313
ls[0] = new ArrayList<>();
1414

15-
//- ls[1] = new ArrayList<Integer>();
15+
// ls[1] = new ArrayList<Integer>();
1616
// error: incompatible types: ArrayList<Integer>
1717
// cannot be converted to List<String>
1818
// ls[1] = new ArrayList<Integer>();
19-
// ^
19+
20+
ls[1] = new ArrayList<String>();
2021

2122
// The problem: List<String> is a subtype of Object
2223
Object[] objects = ls; // So assignment is OK
2324
// Compiles and runs without complaint:
2425
objects[1] = new ArrayList<>();
2526

27+
objects[2] = new ArrayList<Integer>();
28+
2629
// However, if your needs are straightforward it is
2730
// possible to create an array of generics, albeit
2831
// with an "unchecked cast" warning:
2932
List<BerylliumSphere>[] spheres =
3033
(List<BerylliumSphere>[])new List[10];
3134
Arrays.setAll(spheres, n -> new ArrayList<>());
35+
36+
System.out.println(spheres);
37+
3238
}
3339
}

‎arrays/ArraySearching.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static void main(String[] args) {
1515
show("Sorted array", a);
1616
while(true) {
1717
int r = rand.getAsInt();
18+
// r = 1000;
1819
int location = Arrays.binarySearch(a, r);
1920
if(location >= 0) {
2021
System.out.println(

‎arrays/CountUpward.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
public class CountUpward {
1010
static long[] fillCounted(int size) {
11-
return LongStream.iterate(0, i -> i + 1)
11+
return LongStream.iterate(10, i -> i + 2)
1212
.limit(size).toArray();
1313
}
1414
public static void main(String[] args) {
1515
long[] l1 = fillCounted(20); // No problem
1616
show(l1);
1717
// On my machine, this runs out of heap space:
18-
//- long[] l2 = fillCounted(10_000_000);
18+
// long[] l2 = fillCounted(10_000_000);
1919
}
2020
}
2121
/* Output:

‎arrays/SimpleSetAll.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ public static void main(String[] args) {
3232
Arrays.setAll(ia, n -> val++); // [2]
3333
Arrays.setAll(la, n -> val++);
3434
Arrays.setAll(da, n -> val++);
35+
36+
show(ia);
37+
show(la);
38+
show(da);
39+
40+
Arrays.setAll(ia, n -> n*2);
41+
Arrays.setAll(la, n -> n* 2L);
42+
Arrays.setAll(da, n -> n*2);
43+
3544
show(ia);
3645
show(la);
3746
show(da);

‎arrays/StreamFromArray.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Visit http://OnJava8.com for more book information.
55
import java.util.*;
66
import onjava.*;
7+
import onjava.ConvertTo;
78

89
public class StreamFromArray {
910
public static void main(String[] args) {

0 commit comments

Comments
 (0)