Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HelloWorld
- {
- static int[][] arr = new int[4][4];
- public static void main(String[] args)
- {
- arr[0]=new int[]{1,2,3,4};
- arr[1]=new int[]{5,6,7,8};
- arr[2]=new int[]{9,10,11,12};
- arr[3]=new int[]{13,14,15,16};
- int startpos=0;
- int stoppos=arr[0].length;
- while(startpos<stoppos){
- printrow(startpos,stoppos,startpos,true);
- printcol(startpos+1,stoppos-1,stoppos,true);
- printrow(stoppos,startpos,stoppos,false);
- printcol(stoppos-1,startpos+1,startpos,false);
- startpos++;
- stoppos--;
- if(startpos==stoppos){
- System.out.print(arr[startpos][stoppos]);
- break;
- }
- }
- }
- public static void printrow(int startColumnIndex, int stopColumnIndex, int rowindex,boolean increment){
- for(int i=startColumnIndex;;){
- if(increment){
- if(i<=stopColumnIndex){
- System.out.print(arr[rowindex][i]+" ");
- i++;
- }else{
- break;
- }
- }else{
- if(i>=stopColumnIndex){
- System.out.print(arr[rowindex][i]+" ");
- i--;
- }else{
- break;
- }
- }
- }
- }
- public static void printcol(int startRowindex, int stopRowindex, int coulumnindex,boolean increment){
- for(int i=startRowindex;;){
- if(increment){
- if(i<=stopRowindex){
- System.out.print(arr[i][coulumnindex]+" ");
- i++;
- }else{
- break;
- }
- }else{
- if(i>=stopRowindex){
- System.out.print(arr[i][coulumnindex]+" ");
- i--;
- }else{
- break;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment