-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIntArray.java
More file actions
132 lines (113 loc) · 2.79 KB
/
Copy pathIntArray.java
File metadata and controls
132 lines (113 loc) · 2.79 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.util.Arrays;
/**
* A simple array with logging.
*/
public class IntArray {
// +--------+------------------------------------------------------
// | Fields |
// +--------+
/**
* The values in the array.
*/
int[] values;
/**
* The number of calls to get.
*/
int gets;
/**
* The number of calls to set.
*/
int sets;
// +--------------+------------------------------------------------
// | Constructors |
// +--------------+
/**
* Build a new array.
*/
public IntArray(int[] values) {
this.values = values;
this.gets = 0;
this.sets = 0;
} // IntArray(int[])
// +-----------------+---------------------------------------------
// | Primary methods |
// +-----------------+
/**
* Read a value from the array.
*/
public int get(int i) {
++this.gets;
if ((i < 0) || (i >= values.length)) {
String message = "Invalid get at position " + i + " in " + Arrays.toString(values);
throw new ArrayIndexOutOfBoundsException(message);
}
return this.values[i];
} // get(int)
/**
* Write a value to the array.
*/
public void set(int i, int val) {
if ((i < 0) || (i >= values.length)) {
String message = "Invalid set at position " + i + " in " + Arrays.toString(values);
throw new ArrayIndexOutOfBoundsException(message);
}
++this.sets;
this.values[i] = val;
} // set(int, int)
/**
* Determine how many values are in the array.
*/
int length() {
return this.values.length;
} // length()
// +-----------+---------------------------------------------------
// | Utilities |
// +-----------+
/**
* Swap the values at positions i and j.
*/
public void swap(int i, int j) {
int tmp = this.get(i);
this.set(i, this.get(j));
this.set(j, tmp);
} // swap(int, int)
/**
* Make a copy of this array.
*/
public IntArray clone() {
return new IntArray(Arrays.copyOf(this.values, this.values.length));
} // clone
/**
* Permute the array, without counting the sets and gets.
*/
public void permute() {
int sets = this.sets;
int gets = this.gets;
for (int i = 0; i < this.values.length; i++) {
this.swap(i, (int) Math.floor(Math.random() * this.values.length));
} // for
this.sets = sets;
this.gets = gets;
} // permute
/**
* Convert to a string.
*/
public String toString() {
return Arrays.toString(this.values);
} // toString()
// +--------------+------------------------------------------------
// | Logging info |
// +--------------+
/**
* Determine the number of calls to get.
*/
public int gets() {
return this.gets;
} // gets()
/**
* Determine the number of calls to set.
*/
public int sets() {
return this.sets;
} // sets()
} // class IntArray