Guest User

Untitled

a guest
Nov 21st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. public class HelloWorld
  2. {
  3. static int[][] arr = new int[4][4];
  4.  
  5. public static void main(String[] args)
  6. {
  7. arr[0]=new int[]{1,2,3,4};
  8. arr[1]=new int[]{5,6,7,8};
  9. arr[2]=new int[]{9,10,11,12};
  10. arr[3]=new int[]{13,14,15,16};
  11.  
  12. int startpos=0;
  13. int stoppos=arr[0].length;
  14.  
  15. while(startpos<stoppos){
  16. printrow(startpos,stoppos,startpos,true);
  17. printcol(startpos+1,stoppos-1,stoppos,true);
  18. printrow(stoppos,startpos,stoppos,false);
  19. printcol(stoppos-1,startpos+1,startpos,false);
  20. startpos++;
  21. stoppos--;
  22. if(startpos==stoppos){
  23. System.out.print(arr[startpos][stoppos]);
  24. break;
  25. }
  26. }
  27. }
  28.  
  29. public static void printrow(int startColumnIndex, int stopColumnIndex, int rowindex,boolean increment){
  30. for(int i=startColumnIndex;;){
  31. if(increment){
  32. if(i<=stopColumnIndex){
  33. System.out.print(arr[rowindex][i]+" ");
  34. i++;
  35. }else{
  36. break;
  37. }
  38. }else{
  39. if(i>=stopColumnIndex){
  40. System.out.print(arr[rowindex][i]+" ");
  41. i--;
  42. }else{
  43. break;
  44. }
  45. }
  46. }
  47. }
  48.  
  49.  
  50. public static void printcol(int startRowindex, int stopRowindex, int coulumnindex,boolean increment){
  51. for(int i=startRowindex;;){
  52. if(increment){
  53. if(i<=stopRowindex){
  54. System.out.print(arr[i][coulumnindex]+" ");
  55. i++;
  56. }else{
  57. break;
  58. }
  59. }else{
  60. if(i>=stopRowindex){
  61. System.out.print(arr[i][coulumnindex]+" ");
  62. i--;
  63. }else{
  64. break;
  65. }
  66. }
  67. }
  68. }
  69.  
  70.  
  71. }
Add Comment
Please, Sign In to add comment