Java Reference
In-Depth Information
9.
//
array should be
.
10.
//
Variable ”data” stores the data
.
11.
private int
[] data;
12.
//
these variables store the start and end index and the
13.
//
length of the array
14.
private int
startindex, endindex, length;
15.
16.
public
FlexArray(
int
s,
int
e) {
17.
if
(s > e){
18.
System.out.println("ERROR in FlexArray: Start index > end index");
19.
}
20.
else
21.
{
22.
startindex = s;
23.
endindex
= e;
24.
length
= endindex - startindex + 1;
25.
//
In the next command the variable ”data”
26.
//
is defined. We now know how long the
27.
//
array has to be. Then ”data” is no longer
28.
//
null but it references an integer array.
29.
data =
new int
[length];
30.
}//
if
31.
}//
constructor
32.
33.
34.
35.
private int
indexingFunction(
int
c){
36.
int
result = -1;
37.
if
((c < startindex) || (c > endindex)){
38.
System.out.println("ERROR in FlexArray: Illegal index: "+c
39.
+" not in ["+startindex+","+endindex+"]");
40.
}
41.
else
42.
{
43.
result=c-startindex;
44.
}//
if
45.
46.
return
(result);
47.
}
48.
49.
50.
51.
public void
setValue(
int
c,
int
val)
52.
{
53.
if
(indexOK(c)){
54.
data[indexingFunction(c)] = val;
55.
}
Search WWH ::
Custom Search