2nd round print matrix in spiral order
Sigiloso
import java.util.Stack; /** * Created by Anand on 10/8/2016. */ public class SpiralPrintMatrix { public static void main(String[] args) { int[][] array = {{1,2,3,4,56,56},{5,6,7,8,66,66},{9,10,11,12,76,76},{13,14,15,16,86,86},{17,18,19,20,96,96},{21,22,23,24,106,106}}; /* Start moving from (0,0) in the order right, down, left, up and push the elements into the stack simultaneously */ Stack stack = new Stack(); int a = 0, b = 1; // corner values of the matrix int leftup = 0, rightup = array[0].length - 1, leftdown = 0, rightdown = array[0].length - 1; for (int i = 0; i = leftdown) { stack.push(array[rightdown][a]); a--; } rightdown--; a = a + 2; break; // Upward Movement case 3 :while (b >= leftup) { stack.push(array[b][leftdown]); b--; } leftdown++; b = b + 2; break; default: break; } } // Print stack elements while (!stack.isEmpty()) { System.out.print(stack.pop() + " "); } } }