Advertisement
veryinnocentboy

2dmatrixPatterPrint01

Jun 24th, 2021
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. /* "static void main" must be defined in a public class.
  2.  
  3.  
  4. */
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         System.out.println("Hello World!");
  8.         Main main = new Main();
  9.         int[][] arr = new int[][]{
  10.             // {1,2,3,4,5,6,7,8},
  11.             // {6,7,8,9,10},
  12.             // {11,12,13,14,15},
  13.             // {16,17,18,19,20}
  14.            
  15.            
  16.             {1,2,3,4,5},
  17.             {6,7,8,9,10},
  18.             {11,12,13,14,15},
  19.             {16,17,18,19,20}
  20.         };
  21.         main.action(arr);
  22.         main.spiralPrint(arr);
  23.     }
  24.     public void spiralPrint(int[][] arr){
  25.         int height = arr.length;
  26.         int width = arr[0].length;
  27.         int cs = 0;
  28.         int ce = width-1;
  29.         int rs = 0;
  30.         int re = height-1;
  31.         int i,j;
  32.         while(rs < re && cs < ce){
  33.  
  34.             j=cs;
  35.             i=rs;
  36.             while(j <= ce){
  37.                 System.out.print(String.format("%s ",arr[i][j]));
  38.                 j++;
  39.             }
  40.             rs+=1;
  41.             i=rs;
  42.             j=ce;
  43.             while(i <= re){
  44.                 System.out.print(String.format("%s ",arr[i][j]));
  45.                 i++;
  46.             }
  47.             ce-=1;
  48.             i=re;
  49.             j=ce;
  50.             while(j>=cs){
  51.                 System.out.print(String.format("%s ",arr[i][j]));
  52.                 j--;
  53.             }
  54.             re-=1;
  55.             i=re;
  56.             j=cs;
  57.             while(i>=rs){
  58.                 System.out.print(String.format("%s ",arr[i][j]));
  59.                 i--;
  60.             }
  61.             cs+=1;  
  62.         }
  63.     }
  64.     public void action(int [][] arr){
  65.         // print diagonal in zig - zag pattern
  66.         /*
  67.         [1]
  68.         [6, 2]
  69.         [3, 7, 11]
  70.         [16, 12, 8, 4]
  71.         [5, 9, 13, 17]
  72.         [18, 14, 10]
  73.         [15, 19]
  74.         [20]
  75.         */
  76.         int width = arr.length;
  77.         int height =arr[0].length;
  78.        List[] arli = new List[width+height-1];
  79.         for(int i=0;i<width;++i){
  80.             for(int j=0;j<height;++j){
  81.                 if(arli[i+j] == null){
  82.                     arli[i+j] = new ArrayList();
  83.                 }
  84.                 arli[i+j].add(arr[i][j]);
  85.             }
  86.         }
  87.         int count=0;
  88.         for(List li : arli){
  89.             if(count%2==1)
  90.                 Collections.reverse(li);
  91.             System.out.println(li);
  92.            
  93.             count++;
  94.         }
  95.     }
  96.    
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement