Loop diagonally through two dimensional array

前端 未结 12 1313
刺人心
刺人心 2020-12-05 11:52

I wrote the following code to walk half the diagonals of an array:

String[][] b = [a,b,c]
               [d,e,f]
               [g,h,i];  

public void LoopD         


        
12条回答
  •  無奈伤痛
    2020-12-05 11:54

    Here is the code:

    public void loopDiag(String [][] b) {
    
            boolean isPrinted  = false;
            for (int i = 0 ; i < b.length ; i++) {
                String temp="";
                int x=i;
                for(int j = 0 ; j < b.length ; j++) {
                    int y = j;
                    while (x >= 0 && y < b.length) {
                        isPrinted = false;
                        temp+=b[x--][y++];                  
                    }
                    if(!isPrinted) {
                        System.out.println(temp);
                        isPrinted = true;
                    }
                }
            }
        }
    

提交回复
热议问题