我是一个非常新的 java 用户,我在测试我的程序时总是遇到麻烦。在这里,我实现了一个基本的布尔数组接口。我想测试它购买添加几个值,然后运行每个接口方法。请善待我在这方面不是很擅长,但我想变得更好。
package booleanmatrix;
import java.util.Arrays;
/**
*
* @author David
*/
public class ArrayMatrix implements BooleanMatrixs {
private boolean a[][];
public int Rows;
public int Cols;
public int capacityr = 1;
public int capacityc = 1;
public int pT=0;
public int pF=0;
public ArrayMatrix() {
a = new boolean[capacityr][capacityc];
}
@Override
public int getNumberRows() {
return capacityr;
}
@Override
public int getNumberCols() {
return capacityc;
}
@Override
public void set(int row, int col) throws IndexOutOfBoundsException {
if (row > capacityr) {
capacityr = row;
boolean B[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
B[k][j] = a[k][j];
}
}
a = B;
}
if (col > capacityc) {
capacityc = col;
boolean C[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
C[k][j] = a[k][j];
}
}
a = C;
}
a[row][col] = true;
pT++;
}
@Override
public void clear(int row, int col) throws IndexOutOfBoundsException {
if (row > capacityr) {
capacityr = row;
boolean B[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
B[k][j] = a[k][j];
}
}
a = B;
}
if (col > capacityc) {
capacityc = col;
boolean C[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
C[k][j] = a[k][j];
}
}
a = C;
}
a[row][col] = false;
}
@Override
public void set(int row, int col, boolean value) throws IndexOutOfBoundsException {
if (row > capacityr) {
capacityr = row;
boolean B[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
B[k][j] = a[k][j];
}
}
a = B;
}
if (col > capacityc) {
capacityc = col;
boolean C[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
C[k][j] = a[k][j];
}
}
a = C;
}
a[row][col] = value;
if(value==true){
pT++;
}
}
@Override
public void toggle(int row, int col) throws IndexOutOfBoundsException {
if (row > capacityr) {
capacityr = row;
boolean B[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
B[k][j] = a[k][j];
}
}
a = B;
}
if (col > capacityc) {
capacityc = col;
boolean C[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
C[k][j] = a[k][j];
}
}
a = C;
}
if (a[row][col] == true) {
a[row][col] = false;
pT--;
} else {
a[row][col] = true;
pT++;
}
}
@Override
public void setAll() {
Arrays.fill(a, Boolean.TRUE);
pT=(capacityr*capacityc);
}
@Override
public void clearAll() {
Arrays.fill(a, Boolean.FALSE);
pT=0;
}
@Override
public void setAll(boolean value) {
if (value == false) {
Arrays.fill(a, Boolean.FALSE);
} else {
Arrays.fill(a, Boolean.TRUE);
pT=(capacityr*capacityc);
}
}
@Override
public boolean get(int row, int col) throws IndexOutOfBoundsException {
boolean x;
x = a[row][col];
return x;
}
@Override
public int[][] getTruePositions() {
int count = 0;
int ret[][] = new int[pT][2];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
if (a[k][j] == true) {
ret[count][0] = k;
ret[count][1] = j;
count++;
}
}
}
return ret;
}
@Override
public int[][] getFalsePositions() {
int total = (capacityr*capacityc);
int P=(total-pT);
int count=0;
int ret[][] = new int[P][2];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
if (a[k][j] == false) {
ret[count][0] = k;
ret[count][1] = j;
count++;
}
}
}
return ret;
}
@Override
public int[][] getPositions(boolean value) {
int total = (capacityr*capacityc);
int P=(total-pT);
int count=0;
if(value==false){
int retf[][] = new int[P][2];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
if (a[k][j] == false) {
retf[count][0] = k;
retf[count][1] = j;
count++;
}
}
}
return retf;}
else{
int count2 = 0;
int ret[][] = new int[pT][2];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
if (a[k][j] == true) {
ret[count2][0] = k;
ret[count2][1] = j;
count2++;
}
}
}
return ret;
}}
@Override
public int getNumberTrueValues() {
return pT;
}
@Override
public int getNumberFalseValues() {
int total = (capacityr*capacityc);
int P=(total-pT);
return P;
}
@Override
public int getNumberValues(boolean value) {
if (value==false){
int total = (capacityr*capacityc);
int P=(total-pT);
return P;
}
else{return pT;}}
@Override
public String toString(){
String X= "1)The number of rows"+capacityr+"\n 2)The number of Columns"+capacityc+"\n 3)The number of True Values"+pT+"\n 4)The number of false values"+(capacityc*capacityr-pT);
return X;
}
public static void main(String[] args) {
}
}