-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
58 lines (43 loc) · 1.78 KB
/
Main.java
File metadata and controls
58 lines (43 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.dj;
import java.util.Arrays;
public class Main {
record Person(String name, String dob, Person[] kids) {
public Person(Person p) {
this(p.name, p.dob, p.kids == null ? null : Arrays.copyOf(p.kids, p.kids.length));
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", kids=" + Arrays.toString(kids) +
'}';
}
}
public static void main(String[] args) {
Person joe = new Person("Joe", "01/01/1961", null);
Person jim = new Person("Jim", "02/02/1962", null);
Person jack = new Person("Jack", "03/03/1963", new Person[]{joe, jim});
Person jane = new Person("Jane", "04/04/1964", null);
Person jill = new Person("Jill", "05/05/1965", new Person[]{joe, jim});
Person[] persons = {joe, jim, jack, jane, jill};
Person[] personsCopy = persons.clone();
// Person[] personsCopy = Arrays.copyOf(persons, persons.length);
// Person[] personsCopy = new Person[5];
// Arrays.setAll(personsCopy, i -> new Person(persons[i]));
// for (int i = 0; i < 5; i++) {
//// Person current = persons[i];
//// var kids = current.kids() == null ? null :
//// Arrays.copyOf(current.kids(), current.kids().length);
// personsCopy[i] = new Person(persons[i]);
// }
var jillsKids = personsCopy[4].kids();
jillsKids[1] = jane;
for (int i = 0; i < 5; i++) {
if (persons[i] == personsCopy[i]) {
System.out.println("Equal References " + persons[i]);
}
}
System.out.println(persons[4]);
System.out.println(personsCopy[4]);
}
}