-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoDimArrayClass.java
187 lines (157 loc) · 6.02 KB
/
TwoDimArrayClass.java
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package package_8;
public class TwoDimArrayClass
{
private static final int DEFAULT_MAX_CAPACITY = 100;
private static final int DEFAULT_MIN_CAPACITY = 3;
public static final int FAILED_ACCESS = -999999;
private int[][] localTD_Array;
private int rowCapacity;
private int colCapacity;
private int maxCapacity;
public TwoDimArrayClass()
{
rowCapacity = DEFAULT_MIN_CAPACITY;
colCapacity = DEFAULT_MIN_CAPACITY;
maxCapacity = DEFAULT_MAX_CAPACITY;
localTD_Array = new int[rowCapacity][colCapacity];
}
public TwoDimArrayClass( int rowCap, int colCap, int maxCap )
{
maxCapacity = maxCap;
if ( rowCap < DEFAULT_MIN_CAPACITY )
{
rowCapacity = DEFAULT_MIN_CAPACITY;
}
else
{
rowCapacity = rowCap;
}
if ( rowCapacity > DEFAULT_MAX_CAPACITY )
{
rowCapacity = DEFAULT_MAX_CAPACITY;
}
else
{
rowCapacity = rowCap;
}
if ( colCap < DEFAULT_MIN_CAPACITY )
{
colCapacity = DEFAULT_MIN_CAPACITY;
}
else
{
colCapacity = colCap;
}
if ( colCap > DEFAULT_MAX_CAPACITY )
{
colCapacity = DEFAULT_MAX_CAPACITY;
}
else
{
colCapacity = colCap;
}
localTD_Array = new int[rowCapacity][colCapacity];
}
public TwoDimArrayClass( TwoDimArrayClass copied )
{
int rowIndex, colIndex;
int[][] tempArray;
rowCapacity = copied.rowCapacity;
colCapacity = copied.colCapacity;
maxCapacity = copied.maxCapacity;
localTD_Array = copied.localTD_Array;
tempArray = new int[rowCapacity][colCapacity];
for (rowIndex = 0; rowIndex < rowCapacity; rowIndex++)
{
for (colIndex = 0; colIndex < colCapacity; colIndex++)
{
tempArray[rowIndex][colIndex] =
localTD_Array[rowIndex][colIndex];
}
}
localTD_Array = tempArray;
}
public int accessItemAt( int rowAccessIndex, int colAccessIndex )
{
if ( (rowAccessIndex < rowCapacity) &&
(colAccessIndex < colCapacity) )
{
return localTD_Array[rowAccessIndex][colAccessIndex];
}
return FAILED_ACCESS;
}
public boolean arrayIndicesWithinMaxBounds( int testedRowIndex,
int testedColIndex )
{
if ( (testedRowIndex < rowCapacity) &&
(testedColIndex < colCapacity) )
{
return true;
}
else
{
return false;
}
}
public void dumpTwoDimArray()
{
int rowIndex, colIndex;
for (rowIndex = 0; rowIndex < rowCapacity; rowIndex++)
{
for (colIndex = 0; colIndex < colCapacity; colIndex++)
{
if (localTD_Array[rowIndex][colIndex]!= FAILED_ACCESS)
{
System.out.print(localTD_Array[rowIndex][colIndex]
+ ",\t");
}
}
System.out.println(" ");
}
}
public int getCurrentRowCapacity()
{
return rowCapacity;
}
public int getCurrentColCapacity()
{
return colCapacity;
}
public void resize( int newRowCapacity, int newColCapacity )
{
int[][] tempArray;
int rowIndex, colIndex;
tempArray = new int[newRowCapacity][newColCapacity];
for ( rowIndex = 0; rowIndex < rowCapacity; rowIndex++)
{
for ( colIndex = 0; colIndex < colCapacity;
colIndex++)
{
tempArray[rowIndex][colIndex] =
localTD_Array[rowIndex][colIndex];
}
}
localTD_Array = tempArray;
rowCapacity = newRowCapacity;
colCapacity = newColCapacity;
}
public boolean setItemAt( int rowIndexToSet, int colIndexToSet,
int newValue)
{
if ((rowIndexToSet < rowCapacity) && (colIndexToSet < colCapacity))
{
if (arrayIndicesWithinMaxBounds(rowIndexToSet, colIndexToSet))
{
localTD_Array[rowIndexToSet][colIndexToSet] = newValue;
return true;
}
else
{
resize(rowIndexToSet, colIndexToSet);
localTD_Array[rowIndexToSet][colIndexToSet] = newValue;
return true;
}
}
return false;
}
}