问题描述
String [] [] b = [a,b,c]
[d,e,f]
[g,h,i];
$ b $ public void LoopDiag()
for(int i = b.length - 1; i> 0; i--){
String temp =;
for(int j = 0,x = i; x temp = temp + b [x] [j]
System.out.println(temp)
}
for(int i = 0; i String temp =;
for(int j = 0,y = i; y< = b.length - 1; j ++,y ++){
temp = temp + b [j] [y]
}
System.out.println(temp);
现在输出对角线即当前输出: / p>
g dg aei bf c
如何使其打印其他对角线,即所需的输出:
$ p $ a db gec hf i
初始化数组仅用于测试目的b
$ b
int dim = 5;
char ch ='A';
String [] [] array = new String [dim] [];
for(int i = 0; i array [i] = new String [dim];
for(int j = 0; j array [i] [j] =+ ch;
输出我们的矩阵:$ b $ (int j = 0; j for(int j = 0; j System.out.print(array [i] [j] +);
}
System.out.println();
}
System.out.println(============================);
解决方案
对角线元素索引一个规则 - 它们的总和在一个对角线上是不变的:
VARIANT 1
使用两个循环提取所有对角线。
第一个循环提取对角线的上半部分:
for int k = 0; k for(int j = 0; j int i = k-j;
System.out.print(array [i] [j] +);
}
System.out.println();
$ / code>
第二个循环遍历对角线的下半部分:$ b $对于(int j = 0; j< b
pre $ ; = k; j ++){
int i = k - j;
System.out.print(array [dim - j - 1] [dim - i - 1] +);
}
System.out.println();
$ b $ h $ VARIANT 2
使用一个循环来提取所有的对角线,但是有额外的迭代和一个额外的检查:
}
}
System.out.println();
输出:
ABCDE
FGHIJ
KLMNO
PQRST
UVWXY
============== ==============
A
FB
KGC
PLHD
UQMIE
VRNJ
WSO
XT
Y
String[][] b = [a,b,c]
[d,e,f]
[g,h,i];
public void LoopDiag()
for (int i = b.length - 1; i > 0; i--) {
String temp = "";
for (int j = 0, x = i; x <= b.length - 1; j++, x++) {
temp = temp+b[x][j];
}
System.out.println(temp)
}
for (int i = 0; i <= b.length - 1; i++) {
String temp = "";
for (int j = 0, y = i; y <= b.length - 1; j++, y++) {
temp = temp+b[j][y];
}
System.out.println(temp);
}
}
Right now it prints the diagonals i.e current output:
g dg aei bf c
How do I make it print the other diagonals i.e required output:
a db gec hf i
Initialize array only for test purpose:
int dim = 5;
char ch = 'A';
String[][] array = new String[dim][];
for( int i = 0 ; i < dim ; i++ ) {
array[i] = new String[dim];
for( int j = 0 ; j < dim ; j++, ch++ ) {
array[i][j] = "" + ch;
}
}
Output our matrix:
for( int i = 0 ; i < dim ; i++ ) {
for( int j = 0 ; j < dim ; j++, ch++ ) {
System.out.print( array[i][j] + " " );
}
System.out.println();
}
System.out.println( "============================" );
Solution
Element indexes from diagonals have one rule - their sum is constant on one diagonal:
VARIANT 1
Use two loops to extract all diagonals.
First loop extracts top half of diagonals:
for( int k = 0 ; k < dim ; k++ ) {
for( int j = 0 ; j <= k ; j++ ) {
int i = k - j;
System.out.print( array[i][j] + " " );
}
System.out.println();
}
Second loop iterates on bottom half of diagonals:
for( int k = dim - 2 ; k >= 0 ; k-- ) {
for( int j = 0 ; j <= k ; j++ ) {
int i = k - j;
System.out.print( array[dim - j - 1][dim - i - 1] + " " );
}
System.out.println();
}
VARIANT 2
Use one loop to extract all diagonals, but there are extra iterations and one additional check:
for( int k = 0 ; k < dim * 2 ; k++ ) {
for( int j = 0 ; j <= k ; j++ ) {
int i = k - j;
if( i < dim && j < dim ) {
System.out.print( array[i][j] + " " );
}
}
System.out.println();
}
The output:
A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
============================
A
F B
K G C
P L H D
U Q M I E
V R N J
W S O
X T
Y
这篇关于通过二维数组对角循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!