阵列按顺时针方向旋转 [英] Rotate array clockwise
问题描述
我有一个二维数组,我需要按顺时针方向旋转90度,但我不断收到的ArrayIndexOutOfBounds ...
公众诠释[] [] rorateArray(INT [] [] ARR){
//先更改尺寸的垂直长度的水平长度
//,反之亦然
INT [] [] newArray =新INT [改编[0] .length] [arr.length];
//反转值从按钮开始顺时针旋转90度
//数组顶部和从左至右
INT二= 0;
INT JJ = 0;
的for(int i = 0; I<常用3 [0] .length;我++){
对于(INT J = arr.length-1,J> = 0;的J - ){
newArray [II] [林俊杰] =改编[I] [J]。
JJ ++;
}
二++;
}
返回newArray;
}
我不明白你的循环'的逻辑 - 应该不是很
的for(int i = 0; I<常用3 [0] .length;我++){
对于(INT J = arr.length-1,J> = 0;的J - ){
newArray [I] [J] = ARR [J] [我]
}
}
的各项指标是否上升,如我
在这里,或向下,如Ĵ
这里净额(和无论任何一种或两种需要在分配翻转,例如,使用代替普通的
上的的J arr.length-1-J
=
在分配;-),因为改编
尺寸<$ C一边$ C> arr.length 按改编[0] .length
,反之为 newArray
,这在我看来,ARR 第一个索引>(第二对 newArray
)必须是一个跨越arr.length-1 包括范围从0到,以及其他范围的其他指数。
这是一种基本维度分析(除了维采用的是不同的感觉比一般去与维分析是指物理尺寸,即时间,质量,长度,&C; - )。 翻转,并有问题每一个循环上升或下降取决于可视化正是你的意思,我不是最大的心理可视化所以我认为,在现实生活中,我会尝试这样做的各种变种轴换位,直到我打了的意思之一 - 。)
I have a two dimensional array that I need to rotate 90 degrees clockwise, however I keep getting arrayindexoutofbounds...
public int[][] rorateArray(int[][] arr){
//first change the dimensions vertical length for horizontal length
//and viceversa
int[][] newArray = new int[arr[0].length][arr.length];
//invert values 90 degrees clockwise by starting from button of
//array to top and from left to right
int ii = 0;
int jj = 0;
for(int i=0; i<arr[0].length; i++){
for(int j=arr.length-1; j>=0; j--){
newArray[ii][jj] = arr[i][j];
jj++;
}
ii++;
}
return newArray;
}
I don't understand your loops' logic -- shouldn't it be
for(int i=0; i<arr[0].length; i++){
for(int j=arr.length-1; j>=0; j--){
newArray[i][j] = arr[j][i];
}
}
Net of whether each index goes up, like i
here, or down, like j
here (and of whether either or both need to be "flipped" in the assignment, e.g using arr.length-1-j
in lieu of plain j
on one side of the =
in the assignment;-), since arr
dimensions are arr.length
by arr[0].length
, and vice versa for newArray
, it seems to me that the first index on arr
(second on newArray
) must be the one spanning the range from 0 to arr.length-1
included, and the other range for the other index.
This is a kind of "basic dimensional analysis" (except that "dimension" is used in a different sense than normally goes with "dimensional analysis" which refers to physical dimensions, i.e., time, mass, length, &c;-). The issue of "flipping" and having each loop go up or down depend on visualizing exactly what you mean and I'm not the greatest "mental visualizer" so I think, in real life, I'd try the various variants of this "axis transposition" until I hit the one that's meant;-).
这篇关于阵列按顺时针方向旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!