Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
306
306DataStructures_notes
Commits
6aa6d68b
Commit
6aa6d68b
authored
Mar 23, 2019
by
michael lundquist
Browse files
About to delete this code. Clone is protected in Java's Cloneable interface, so it doesn't work.
parent
36ad56bf
Changes
1
Hide whitespace changes
Inline
Side-by-side
306BookNotes/3_Fundamental_Data_Structures/ch3_code/Examples.java
View file @
6aa6d68b
...
...
@@ -4,26 +4,34 @@ import java.util.Arrays;
import
java.util.Random
;
public
class
Examples
{
public
static
void
main
(
String
[]
args
){
randomExample
();
//CustomS_B[] vars = {new Examples.CustomS_B("abc"),new Examples.CustomS_B("DEF"),new Examples.CustomS_B("ghi")};
//vars.clone();
Integer
[]
vars
=
{
1
,
2
,
3
,
4
};
System
.
out
.
println
(
Arrays
.
toString
(
deepClone
(
vars
)));
}
public
Object
[]
deepClone
(
Object
[]
o
){
Class
oType
=
o
.
getClass
();
Object
[]
newClone
=
Array
.
newInstance
(
oType
,
o
.
length
);
for
(
int
k
=
0
;
k
<
contacts
.
length
;
k
++){
try
{
Method
clone
=
o
[
k
].
getMethod
(
"clone"
,
new
Class
[
0
]);
newClone
[
k
]
=
(
Object
)
clone
.
invoke
(
obj
,
args
);
}
catch
(
NoSuchMethodException
e
){
e
.
printStackTrace
();
/**
* A way of cloning an array of clonables.
* Works with multi dimensional arrays
*/
public
static
Integer
[]
deepClone
(
Integer
[]
o
){
//Class oType = o.getClass();
//Cloneable[] newClone = o.clone();//Array.newInstance(oType, o.length);
Integer
[]
newClone
=
o
.
clone
();
for
(
int
k
=
0
;
k
<
o
.
length
;
k
++){
if
(
o
[
k
].
getClass
().
isArray
()){
newClone
[
k
]
=
deepClone
((
Integer
[])
o
[
k
]);
}
else
{
Cloneable
newObj
=
o
[
k
];
newClone
[
k
]
=
newObj
.
clone
();
}
}
return
newClone
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment