Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* "static void main" must be defined in a public class.
- */
- public class Main {
- public static void main(String[] args) {
- System.out.println("Hello World!");
- Main main = new Main();
- int[][] arr = new int[][]{
- // {1,2,3,4,5,6,7,8},
- // {6,7,8,9,10},
- // {11,12,13,14,15},
- // {16,17,18,19,20}
- {1,2,3,4,5},
- {6,7,8,9,10},
- {11,12,13,14,15},
- {16,17,18,19,20}
- };
- main.action(arr);
- main.spiralPrint(arr);
- }
- public void spiralPrint(int[][] arr){
- int height = arr.length;
- int width = arr[0].length;
- int cs = 0;
- int ce = width-1;
- int rs = 0;
- int re = height-1;
- int i,j;
- while(rs < re && cs < ce){
- j=cs;
- i=rs;
- while(j <= ce){
- System.out.print(String.format("%s ",arr[i][j]));
- j++;
- }
- rs+=1;
- i=rs;
- j=ce;
- while(i <= re){
- System.out.print(String.format("%s ",arr[i][j]));
- i++;
- }
- ce-=1;
- i=re;
- j=ce;
- while(j>=cs){
- System.out.print(String.format("%s ",arr[i][j]));
- j--;
- }
- re-=1;
- i=re;
- j=cs;
- while(i>=rs){
- System.out.print(String.format("%s ",arr[i][j]));
- i--;
- }
- cs+=1;
- }
- }
- public void action(int [][] arr){
- // print diagonal in zig - zag pattern
- /*
- [1]
- [6, 2]
- [3, 7, 11]
- [16, 12, 8, 4]
- [5, 9, 13, 17]
- [18, 14, 10]
- [15, 19]
- [20]
- */
- int width = arr.length;
- int height =arr[0].length;
- List[] arli = new List[width+height-1];
- for(int i=0;i<width;++i){
- for(int j=0;j<height;++j){
- if(arli[i+j] == null){
- arli[i+j] = new ArrayList();
- }
- arli[i+j].add(arr[i][j]);
- }
- }
- int count=0;
- for(List li : arli){
- if(count%2==1)
- Collections.reverse(li);
- System.out.println(li);
- count++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement