环形矩阵

/**
 * 
 * 
 1 1 1 
 1 0 1 
 1 1 1 


 2  2  2  2  2  
 2  1  1  1  2  
 2  1  0  1  2  
 2  1  1  1  2  
 2  2  2  2  2  


 3  3  3  3  3  3  3  
 3  2  2  2  2  2  3  
 3  2  1  1  1  2  3  
 3  2  1  0  1  2  3  
 3  2  1  1  1  2  3  
 3  2  2  2  2  2  3  
 3  3  3  3  3  3  3   
 *
 */
public class SymmetricArray {
 public static void main(String[] args) {
  for (int n = 1; n < 10; n++) {
   System.out.println();
   SymmetricArray sa = new SymmetricArray();
   sa.printArray(sa.populateArray(n), 2 * n + 1);
   System.out.println();
  }
 }
 private int[][] populateArray(int expectedNumber) {
  int length = expectedNumber * 2 + 1;
  int[][] array = new int[length][length];
  for (int i = expectedNumber; i >= 0; i--) {
   for (int j = expectedNumber - i; j < expectedNumber + i + 1; j++) {
    array[j][expectedNumber - i] = array[expectedNumber - i][j] = i;
    array[j][expectedNumber + i] = array[expectedNumber + i][j] = i;
   }
  }
  return array;
 }
 /**
  * 打印二维数组的内容。
  * 
  * @param array
  * @param expectedNumber
  */
 public void printArray(int[][] array, int expectedNumber) {
  for (int i = 0; i < expectedNumber; i++) {
   for (int j = 0; j < expectedNumber; j++) {
    System.out.print(appendSpace(array[i][j], expectedNumber));
   }
   System.out.println();
  }
 }
 /**
  * 为了保证数据输出时能够对齐,添加一些空格.
  */
 private String appendSpace(int value, int expectedNumber) {
  int maxValue = expectedNumber * expectedNumber;
  int maxLength = String.valueOf(maxValue).length();
  int currentValueLength = String.valueOf(value).length();
  StringBuilder sb = new StringBuilder();
  sb.append(value);
  for (int i = maxLength; i > currentValueLength; i--) {
   sb.append(" ");
  }
  sb.append(" ");
  return sb.toString();
 }

}


输出结果: 


1 1 1 
1 0 1 
1 1 1 

2  2  2  2  2  
2  1  1  1  2  
2  1  0  1  2  
2  1  1  1  2  
2  2  2  2  2  

3  3  3  3  3  3  3  
3  2  2  2  2  2  3  
3  2  1  1  1  2  3  
3  2  1  0  1  2  3  
3  2  1  1  1  2  3  
3  2  2  2  2  2  3  
3  3  3  3  3  3  3  

4  4  4  4  4  4  4  4  4  
4  3  3  3  3  3  3  3  4  
4  3  2  2  2  2  2  3  4  
4  3  2  1  1  1  2  3  4  
4  3  2  1  0  1  2  3  4  
4  3  2  1  1  1  2  3  4  
4  3  2  2  2  2  2  3  4  
4  3  3  3  3  3  3  3  4  
4  4  4  4  4  4  4  4  4  

5   5   5   5   5   5   5   5   5   5   5   
5   4   4   4   4   4   4   4   4   4   5   
5   4   3   3   3   3   3   3   3   4   5   
5   4   3   2   2   2   2   2   3   4   5   
5   4   3   2   1   1   1   2   3   4   5   
5   4   3   2   1   0   1   2   3   4   5   
5   4   3   2   1   1   1   2   3   4   5   
5   4   3   2   2   2   2   2   3   4   5   
5   4   3   3   3   3   3   3   3   4   5   
5   4   4   4   4   4   4   4   4   4   5   
5   5   5   5   5   5   5   5   5   5   5   

6   6   6   6   6   6   6   6   6   6   6   6   6   
6   5   5   5   5   5   5   5   5   5   5   5   6   
6   5   4   4   4   4   4   4   4   4   4   5   6   
6   5   4   3   3   3   3   3   3   3   4   5   6   
6   5   4   3   2   2   2   2   2   3   4   5   6   
6   5   4   3   2   1   1   1   2   3   4   5   6   
6   5   4   3   2   1   0   1   2   3   4   5   6   
6   5   4   3   2   1   1   1   2   3   4   5   6   
6   5   4   3   2   2   2   2   2   3   4   5   6   
6   5   4   3   3   3   3   3   3   3   4   5   6   
6   5   4   4   4   4   4   4   4   4   4   5   6   
6   5   5   5   5   5   5   5   5   5   5   5   6   
6   6   6   6   6   6   6   6   6   6   6   6   6   

7   7   7   7   7   7   7   7   7   7   7   7   7   7   7   
7   6   6   6   6   6   6   6   6   6   6   6   6   6   7   
7   6   5   5   5   5   5   5   5   5   5   5   5   6   7   
7   6   5   4   4   4   4   4   4   4   4   4   5   6   7   
7   6   5   4   3   3   3   3   3   3   3   4   5   6   7   
7   6   5   4   3   2   2   2   2   2   3   4   5   6   7   
7   6   5   4   3   2   1   1   1   2   3   4   5   6   7   
7   6   5   4   3   2   1   0   1   2   3   4   5   6   7   
7   6   5   4   3   2   1   1   1   2   3   4   5   6   7   
7   6   5   4   3   2   2   2   2   2   3   4   5   6   7   
7   6   5   4   3   3   3   3   3   3   3   4   5   6   7   
7   6   5   4   4   4   4   4   4   4   4   4   5   6   7   
7   6   5   5   5   5   5   5   5   5   5   5   5   6   7   
7   6   6   6   6   6   6   6   6   6   6   6   6   6   7   
7   7   7   7   7   7   7   7   7   7   7   7   7   7   7   

8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   
8   7   7   7   7   7   7   7   7   7   7   7   7   7   7   7   8   
8   7   6   6   6   6   6   6   6   6   6   6   6   6   6   7   8   
8   7   6   5   5   5   5   5   5   5   5   5   5   5   6   7   8   
8   7   6   5   4   4   4   4   4   4   4   4   4   5   6   7   8   
8   7   6   5   4   3   3   3   3   3   3   3   4   5   6   7   8   
8   7   6   5   4   3   2   2   2   2   2   3   4   5   6   7   8   
8   7   6   5   4   3   2   1   1   1   2   3   4   5   6   7   8   
8   7   6   5   4   3   2   1   0   1   2   3   4   5   6   7   8   
8   7   6   5   4   3   2   1   1   1   2   3   4   5   6   7   8   
8   7   6   5   4   3   2   2   2   2   2   3   4   5   6   7   8   
8   7   6   5   4   3   3   3   3   3   3   3   4   5   6   7   8   
8   7   6   5   4   4   4   4   4   4   4   4   4   5   6   7   8   
8   7   6   5   5   5   5   5   5   5   5   5   5   5   6   7   8   
8   7   6   6   6   6   6   6   6   6   6   6   6   6   6   7   8   
8   7   7   7   7   7   7   7   7   7   7   7   7   7   7   7   8   
8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   

9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   
9   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   9   
9   8   7   7   7   7   7   7   7   7   7   7   7   7   7   7   7   8   9   
9   8   7   6   6   6   6   6   6   6   6   6   6   6   6   6   7   8   9   
9   8   7   6   5   5   5   5   5   5   5   5   5   5   5   6   7   8   9   
9   8   7   6   5   4   4   4   4   4   4   4   4   4   5   6   7   8   9   
9   8   7   6   5   4   3   3   3   3   3   3   3   4   5   6   7   8   9   
9   8   7   6   5   4   3   2   2   2   2   2   3   4   5   6   7   8   9   
9   8   7   6   5   4   3   2   1   1   1   2   3   4   5   6   7   8   9   
9   8   7   6   5   4   3   2   1   0   1   2   3   4   5   6   7   8   9   
9   8   7   6   5   4   3   2   1   1   1   2   3   4   5   6   7   8   9   
9   8   7   6   5   4   3   2   2   2   2   2   3   4   5   6   7   8   9   
9   8   7   6   5   4   3   3   3   3   3   3   3   4   5   6   7   8   9   
9   8   7   6   5   4   4   4   4   4   4   4   4   4   5   6   7   8   9   
9   8   7   6   5   5   5   5   5   5   5   5   5   5   5   6   7   8   9   
9   8   7   6   6   6   6   6   6   6   6   6   6   6   6   6   7   8   9   
9   8   7   7   7   7   7   7   7   7   7   7   7   7   7   7   7   8   9   
9   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   9   
9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值