欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實現(xiàn)的順時針/逆時針打印矩陣操作示例

 更新時間:2019年12月21日 12:28:11   作者:halaoda  
這篇文章主要介紹了java實現(xiàn)的順時針/逆時針打印矩陣操作,涉及java基于數(shù)組的矩陣存儲、遍歷、打印輸出等相關(guān)操作技巧,需要的朋友可以參考下

java實現(xiàn)的順時針/逆時針打印矩陣操作。分享給大家供大家參考,具體如下:

public class SnakeMatrix {
  /**
   * 定義矩陣的階數(shù)
   */
  private int n;
  //填充矩陣的值
  private int k = 1;
  private int[][] data;
  /**
   * 定義矩陣移動的方向
   */
  public enum Direction {
    left, right, up, down,
  }
  SnakeMatrix(int n) {
    this.n = n;
    data = new int[n][n];
  }
  public void clockwisePrintMatrix() {
    //定義行數(shù)
    int rowLen = data.length;
    //定義列數(shù)
    int columnLen = data.length;
    //移動方向
    Direction direction = Direction.right;
    //定義上邊界
    int upBound = 0;
    //定義下邊界
    int downBound = rowLen - 1;
    //定義左邊界
    int leftBound = 0;
    //定義右邊界
    int rightBound = columnLen - 1;
    //矩陣當(dāng)前行數(shù)
    int row = 0;
    //矩陣當(dāng)前列數(shù)
    int column = 0;
    while (true) {
      data[row][column] = k++;
      if (upBound == downBound && leftBound == rightBound) {
        // System.out.println(" upBound :"+upBound +" downBound :"+downBound+" leftBound :"+leftBound +" rightBound :"+rightBound);
        break;
      }
      switch (direction) {
        case right:
          if (column < rightBound) {
            ++column;
          } else {
            ++row;
            direction = Direction.down;
            ++upBound;
          }
          break;
        case down:
          if (row < downBound) {
            ++row;
          } else {
            --column;
            direction = Direction.left;
            --rightBound;
          }
          break;
        case up:
          if (row > upBound) {
            --row;
          } else {
            ++column;
            direction = Direction.right;
            ++leftBound;
          }
          break;
        case left:
          if (column > leftBound) {
            --column;
          } else {
            --row;
            direction = Direction.up;
            --downBound;
          }
          break;
        default:
          break;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        System.out.printf("%2d%s", data[i][j], " ");
      }
      System.out.println();
    }
  }
  public void anticlockwisePrintMatrix() {
    int rowLen = data.length;
    int columnLen = data.length;
    int leftBound = 0;
    int rightBound = columnLen - 1;
    int upBound = 0;
    int downBound = rowLen - 1;
    int row = 0;
    int column = 0;
    Direction direction = Direction.down;
    while (true) {
      data[row][column] = k++;
      if (rightBound == leftBound && upBound == downBound) {
        break;
      }
      switch (direction) {
        case down:
          if (row < downBound) {
            row++;
          } else {
            column++;
            direction = Direction.right;
            leftBound++;
          }
          break;
        case right:
          if (column < rightBound) {
            column++;
          } else {
            row--;
            direction = Direction.up;
            downBound--;
          }
          break;
        case up:
          if (row > upBound) {
            row--;
          } else {
            direction = Direction.left;
            column--;
            rightBound--;
          }
          break;
        case left:
          if (column > leftBound) {
            column--;
          } else {
            direction = Direction.down;
            row++;
            upBound++;
          }
          break;
        default:
          break;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        System.out.printf("%2d%s", data[i][j], " ");
      }
      System.out.println();
    }
  }
}

首先呢上面是定義一個工具類,

public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int number = 5;
    SnakeMatrix snakeMatrix = new SnakeMatrix(number);
    snakeMatrix.anticlockwisePrintMatrix();
    //snakeMatrix.clockwisePrintMatrix();
  }
}

直接進(jìn)行使用,有兩個方法,一個正序一個倒序

更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計有所幫助。

相關(guān)文章

  • Java流程控制之順序結(jié)構(gòu)

    Java流程控制之順序結(jié)構(gòu)

    Java中的流程控制語句可以這樣分類:順序結(jié)構(gòu),選擇結(jié)構(gòu),循環(huán)結(jié)構(gòu)。下面文章我們就來看看來順序結(jié)構(gòu)的詳細(xì)介紹,主要以順序結(jié)構(gòu)簡單圖示及詳細(xì)解說該內(nèi)容,需要的小伙伴可以參考一下
    2021-12-12
  • Spring的IOC控制反轉(zhuǎn)詳解

    Spring的IOC控制反轉(zhuǎn)詳解

    這篇文章主要為大家介紹了Spring的IOC控制反轉(zhuǎn),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助<BR>
    2022-01-01
  • Java中定時任務(wù)的6種實現(xiàn)方式

    Java中定時任務(wù)的6種實現(xiàn)方式

    這篇文章主要給大家分享的是Java中定時任務(wù)的6種實現(xiàn)方式,幾乎在所有的項目中,定時任務(wù)的使用都是不可或缺的,如果使用不當(dāng)甚至?xí)斐少Y損,下面文章我們就來看看Java中定時任務(wù)的具體使用方式吧
    2021-10-10
  • SpringBoot多數(shù)據(jù)源配置的全過程記錄

    SpringBoot多數(shù)據(jù)源配置的全過程記錄

    在用SpringBoot開發(fā)項目時,隨著業(yè)務(wù)量的擴(kuò)大,我們通常會進(jìn)行數(shù)據(jù)庫拆分或是引入其他數(shù)據(jù)庫,從而我們需要配置多個數(shù)據(jù)源,下面這篇文章主要給大家介紹了關(guān)于SpringBoot多數(shù)據(jù)源配置的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • Java Adapter 適配器模式(類適配器,對象適配器)優(yōu)缺點對比

    Java Adapter 適配器模式(類適配器,對象適配器)優(yōu)缺點對比

    這篇文章主要介紹了Java 適配器模式(類適配器,對象適配器)優(yōu)缺點對比的相關(guān)資料,java 適配器在基礎(chǔ)知識中還是比較重要的,這里就說下如何使用,需要的朋友可以參考下
    2016-12-12
  • 使用mybatis進(jìn)行數(shù)據(jù)插入時返回自增id的方法及注意點

    使用mybatis進(jìn)行數(shù)據(jù)插入時返回自增id的方法及注意點

    這篇文章主要給大家介紹了關(guān)于使用mybatis進(jìn)行數(shù)據(jù)插入時返回自增id的方法及注意點,在插入一條數(shù)據(jù)之后需要返回它的自增主鍵id,因為插入的實體類數(shù)據(jù)id為空,后面的邏輯還需要這個id,需要的朋友可以參考下
    2023-09-09
  • 一文詳解Mybatis-plus的介紹與使用

    一文詳解Mybatis-plus的介紹與使用

    Mybatis-Plus?是?MyBatis?的一個增強(qiáng)工具,專門針對于傳統(tǒng)MyBatis開發(fā)中sql需要手動進(jìn)行映射配置繁瑣缺點的一款框架技術(shù)。本文將為大家詳細(xì)講講Mybatis-plus的介紹與使用,感興趣的可以了解一下
    2022-07-07
  • SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫

    SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫

    這篇文章主要介紹了SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • 詳解SpringMVC常用注解功能及屬性

    詳解SpringMVC常用注解功能及屬性

    這篇文章主要介紹了詳解SpringMVC注解功能及屬性,文中通過詳細(xì)的示例代碼作了簡要的分析,有需要的朋友可以借鑒參考下,希望可以有所幫助
    2021-09-09
  • Java之不通過構(gòu)造函數(shù)創(chuàng)建一個對象問題

    Java之不通過構(gòu)造函數(shù)創(chuàng)建一個對象問題

    這篇文章主要介紹了Java之不通過構(gòu)造函數(shù)創(chuàng)建一個對象問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評論