[セットトップ]java実装スパイラル配列-別の方法


Javaの螺旋の配列に値を割り当てる时、ネット上ですべてデータの间の规则を探し当てて、それから、规则を通じてデータの元素に値を与えて、それから、同僚に教えてもらって、同僚は私に别の方法を提供して、个人の感じはとても悪くなくて、分かち合いました:(これは反时计回りの螺旋のデータです)
         この方法はn*n型の2次元配列だけでなく,m*n型の螺旋配列の付与も解決できる.
         コードは次のとおりです.
        
/**
 *        
 * @author Administrator
 *
 */
public class LoopAndRecursionACW2 {
	private int[][] numArray;
	int x=0;
	int y=0;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LoopAndRecursionACW2 loopACW = new LoopAndRecursionACW2();
		loopACW.createArray();
		loopACW.printToConsole();
	}
	private void createArray() {
		int i=0,j=0;//    
		int c=0;//  , 0  ,          (    ),      ,       ,    
		int d=0;//0,      ,1,      ,2,     ,3,     
		String s = JOptionPane.showInputDialog("      ,    ");
		String[] a = s.split(",");
		x = Integer.parseInt(a[0]);
		y = Integer.parseInt(a[1]);
		numArray = new int[x][y];
	    int maxValue = x*y;
	    
	    for(int k=1;k<=maxValue;k++){
	    	if(d==0){
	    		if(i==x-c-1){//     
	    			d=1;
	    		}else{
	    			numArray[i][j]=k;
	    			i++;
	    			continue;
	    		}
	    	}
	    	if(d==1){
	    		if(j==y-c-1){//     
	    			d=2;
	    		}else{
	    			numArray[i][j]=k;
	    			j++;
	    			continue;
	    		}
	    	}
	    	if(d==2){
	    		if(i==c){//     
	    			d=3;
	    		}else{
	    			numArray[i][j]=k;
	    			i--;
	    			continue;
	    		}
	    	}
	    	if(d==3){
	    		if(j==c+1){//            
	    			numArray[i][j]=k;
	    			d=0;
	    			c++;
	    			i++;
	    			continue;
	    		}else{
	    			numArray[i][j]=k;
	    			j--;
	    			continue;
	    		}
	    	}  	
	    }
		
	}
	
	private void printToConsole() {
		String space = "";
		for(int i=0;i<x;i++){
			for(int j=0;j<y;j++){
				if(numArray[i][j]<10){
					space="  ";
				}else{
					space=" ";
				}
				System.out.print(numArray[i][j]+space);
			}
			System.out.println();//  
		}
		
	}


}

以下は、n*n次元のスパイラル配列の付与のみを実現するネット上で一般的な方法です.コードは次のとおりです.
/**
 *          
 * ACW-AntiClockWise
 * @author Administrator
 *
 */
public class LoopAndRecursionACW {

	int[][] numArray;
	int record = 1;//          
	int stepF = 0;//        
	int stepE = 0;//        
	int reduce = 0;//        

	public static void main(String args[]) {

		new LoopAndRecursionACW().process();

	}

	public void setNum(int num) {//           
		if (num > 0) {//            

			for (int i = stepF; i < stepE; i++) {//               
				numArray[i][stepF] = record;
				numArray[i][stepE - 1] = record + 3 * (num - 1) - 2 * reduce;
				reduce++;
				record++;
			}
			reduce = 1;
			for (int i = stepF + 1; i < stepE - 1; i++) {//               
				numArray[stepE - 1][i] = record;
				numArray[stepF][i] = record + 3 * (num - 1) - 2 * reduce;
				reduce++;
				record++;
			}
			reduce = 0;
			stepF++;
			record = record + 2 * (num - 1);//          
			stepE--;
			setNum(num - 2);//                2
		} else
			//                 
			return;
	}

	public void process() {//                
		String s = JOptionPane.showInputDialog("    ");
		int num = Integer.parseInt(s);
		numArray = new int[num][num];
		String space = "";
		stepE = num;//       
		setNum(num);
		for (int i = 0; i < num; i++) {
			for (int j = 0; j < num; j++) {
				if (numArray[i][j] < 10 && j > 0) {//          ,          2      
					space = "  ";
				} else
					space = " ";
				System.out.print(numArray[i][j] + space);
			}
			System.out.println();//          
		}
	}

}

まとめ:真剣に分析すれば、第1の方法の理解と使用範囲は第2の方法よりずっとよく、第2の方法の数学の法則は探しにくいことがわかります.