我写了以下代码:
public class Matrix
{
private final int[][] array;
private final int size1;
private final int size2;
Matrix(int size1,int size2)
{
this.size1=size1;
this.size2=size2;
this.array=new int[size1][size2];
}
Matrix(int[][] color)
{
this.size1=color.length;
this.size2=color[0].length;
this.array= new int[size1][size2];
for(int row=0;row<size1;row++)
{
for(int column=0;column<size2;column++)
{
this.array[row][column]=color[row][column];
}
}
}
}
public Matrix flipVertically()
{
Matrix newArray=new Matrix(array);
for(int row=size1-1;row>=0;row--)
{
for(int column=0;column<array[row].length;column++)
{
newArray[row][column]=array[size1][size2];
}
}
return newArray;
}
}
并且我的方法需要返回一个新的Matrix,在它的内部,第一行成为最后一行,第二行成为倒数第二行,依此类推...
我写了这段代码,但是有两个问题:
当我写newArray[row][column]=array[size1][size2]
时;
它写道:矩阵是必需的,但找到了数组。为什么写呢?以及我该如何解决?
返回“ Metrix”变量是什么意思?看起来如何?
谢谢!
答案 0 :(得分:1)
您的问题是,您正在像对待Matrix
的实例一样对待它,是一个数组;它不是。它有一个数组。如果要操纵newArray
实例的内部数组(强烈建议将变量重命名为newMatrix
),则可以通过访问newArray.array
来实现。
答案 1 :(得分:0)
如果我正确的话,这就是您想要的:
public class Matrix {
private final int[][] array;
private final int size1;
private final int size2;
Matrix(int size1, int size2) {
this.size1 = size1;
this.size2 = size2;
this.array = new int[size1][size2];
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (int[] arr: array)
builder.append(Arrays.toString(arr) + "\n");
return "Matrix{" +
"array=\n" + builder +
'}';
}
Matrix(int[][] color) {
this.size1 = color.length;
this.size2 = color[0].length;
this.array = new int[size1][size2];
for (int row = 0; row < size1; row++) {
for (int column = 0; column < size2; column++) {
this.array[row][column] = color[row][column];
}
}
}
public Matrix flipVertically() {
int start = 0, end = size1 - 1;
while (start < end) {
int[] tmp = array[start];
array[start] = array[end];
array[end] = tmp;
start++;
end--;
}
return this;
}
public static void main(String[] args) {
Matrix matrix = new Matrix(new int[][]{{1,2,3}, {4,5,6}, {7,8,9}});
System.out.println(matrix.flipVertically());
matrix = new Matrix(new int[][]{{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}});
System.out.println(matrix.flipVertically());
matrix = new Matrix(new int[][]Matrix{array=
[7, 8, 9]
[4, 5, 6]
[1, 2, 3]
}
Matrix{array=
[10, 11, 12]
[7, 8, 9]
[4, 5, 6]
[1, 2, 3]
}
Matrix{array=
[1]
}
Matrix{array=
[3]
[2]
[1]
}
);
System.out.println(matrix.flipVertically());
matrix = new Matrix(new int[][]{{1},{2},{3}});
System.out.println(matrix.flipVertically());
}
输出:
newArray[row][column]=array[size1][size2];
要回答您的问题:
Matrix matrix = new Matrix(new int[][]{{1,2,3}, {4,5,6},
{7,8,9}});
是
不是我如何在Java中复制数组,尽管我不认为你
需要完全复制它-请参阅我的实现。反正要复制
您的阵列,请参考此
copy a 2d array in java this
。在我的代码中,我只返回friendly
女巫是对
本身。 P.S。改进代码的建议:添加getter和setter,如果没有理由保留<Spc>
<Name>Perim West_RM000001</Name>
<CondgType>DirectlyConditioned</CondgType>
<SupPlenumSpcRef>- none -</SupPlenumSpcRef>
<RetPlenumSpcRef>- none -</RetPlenumSpcRef>
<ThrmlZnRef>Zn Perim West</ThrmlZnRef>
<IntWall>
<Name>RM000001_W40</Name>
<Status>New</Status>
<AdjacentSpcRef>Perim South_RM000004</AdjacentSpcRef>
<ConsAssmRef>2013 Internal Partition</ConsAssmRef>
</IntWall>
<ExtWall>
<Name>RM000001_W12</Name>
<Status>New</Status>
<ConsAssmRef>2013 External Wall</ConsAssmRef>
<Win>
<Name>RM000001_W1-W0</Name>
<Status>New</Status>
<FenConsRef>2013 External Window</FenConsRef>
</Win>
</Spc>
,则将构造函数公开,在将空数组传递给构造函数时检查极端情况。...