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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
object Test extends App{
BooleanArrayClone;
ByteArrayClone;
ShortArrayClone;
CharArrayClone;
IntArrayClone;
LongArrayClone;
FloatArrayClone;
DoubleArrayClone;
ObjectArrayClone;
PolymorphicArrayClone;
}
object BooleanArrayClone{
val it : Array[Boolean] = Array(true, false);
val cloned = it.clone();
assert(cloned.sameElements(it));
cloned(0) = false;
assert(it(0) == true)
}
object ByteArrayClone{
val it : Array[Byte] = Array(1, 0);
val cloned = it.clone();
assert(cloned.sameElements(it));
cloned(0) = 0;
assert(it(0) == 1)
}
object ShortArrayClone{
val it : Array[Short] = Array(1, 0);
val cloned = it.clone();
assert(cloned.sameElements(it));
cloned(0) = 0;
assert(it(0) == 1)
}
object CharArrayClone{
val it : Array[Char] = Array(1, 0);
val cloned = it.clone();
assert(cloned.sameElements(it));
cloned(0) = 0;
assert(it(0) == 1)
}
object IntArrayClone{
val it : Array[Int] = Array(1, 0);
val cloned = it.clone();
assert(cloned.sameElements(it));
cloned(0) = 0;
assert(it(0) == 1)
}
object LongArrayClone{
val it : Array[Long] = Array(1, 0);
val cloned = it.clone();
assert(cloned.sameElements(it));
cloned(0) = 0;
assert(it(0) == 1)
}
object FloatArrayClone{
val it : Array[Float] = Array(1, 0);
val cloned = it.clone();
assert(cloned.sameElements(it));
cloned(0) = 0;
assert(it(0) == 1)
}
object DoubleArrayClone{
val it : Array[Double] = Array(1, 0);
val cloned = it.clone();
assert(cloned.sameElements(it));
cloned(0) = 0;
assert(it(0) == 1)
}
object ObjectArrayClone{
val it : Array[String] = Array("1", "0");
val cloned = it.clone();
assert(cloned.sameElements(it));
cloned(0) = "0";
assert(it(0) == "1")
}
object PolymorphicArrayClone{
def testIt[T](it : Array[T], one : T, zero : T) = {
val cloned = it.clone();
assert(cloned.sameElements(it));
cloned(0) = zero;
assert(it(0) == one)
}
testIt(Array("one", "two"), "one", "two");
class Mangler[T: Manifest](ts : T*){
// this will always be a BoxedAnyArray even after we've unboxed its contents.
val it = ts.toArray[T];
}
val mangled = new Mangler[Int](0, 1);
val y : Array[Int] = mangled.it; // make sure it's unboxed
testIt(mangled.it, 0, 1);
}
|