最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實現的順時針/逆時針打印矩陣操作示例

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

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

public class SnakeMatrix {
  /**
   * 定義矩陣的階數
   */
  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() {
    //定義行數
    int rowLen = data.length;
    //定義列數
    int columnLen = data.length;
    //移動方向
    Direction direction = Direction.right;
    //定義上邊界
    int upBound = 0;
    //定義下邊界
    int downBound = rowLen - 1;
    //定義左邊界
    int leftBound = 0;
    //定義右邊界
    int rightBound = columnLen - 1;
    //矩陣當前行數
    int row = 0;
    //矩陣當前列數
    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();
  }
}

直接進行使用,有兩個方法,一個正序一個倒序

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

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

相關文章

  • java 串口通信實現流程示例

    java 串口通信實現流程示例

    這篇文章主要介紹了java 串口通信實現流程示例,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Java學習之反射機制及應用場景介紹

    Java學習之反射機制及應用場景介紹

    本篇文章主要介紹了Java反射機制及應用場景,反射機制是很多Java框架的基石。非常具有實用價值,需要的朋友可以參考下。
    2016-11-11
  • SpringBoot項目使用aop案例詳解

    SpringBoot項目使用aop案例詳解

    這篇文章主要介紹了SpringBoot項目使用aop的相關知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • java讀寫二進制文件的解決方法

    java讀寫二進制文件的解決方法

    本篇文章是對java讀寫二進制文件的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • ShardingSphere如何進行sql重寫示例詳解

    ShardingSphere如何進行sql重寫示例詳解

    這篇文章主要為大家介紹了ShardingSphere如何進行sql重寫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • SpringBoot中使用@ControllerAdvice注解詳解

    SpringBoot中使用@ControllerAdvice注解詳解

    這篇文章主要介紹了SpringBoot中使用@ControllerAdvice注解詳解,@ControllerAdvice,是Spring3.2提供的新注解,它是一個Controller增強器,可對controller中被 @RequestMapping注解的方法加一些邏輯處理,需要的朋友可以參考下
    2023-10-10
  • 深入分析Java內存區(qū)域的使用詳解

    深入分析Java內存區(qū)域的使用詳解

    本篇文章對Java內存區(qū)域的使用進行了詳細的介紹。需要的朋友參考下
    2013-05-05
  • Nacos與SpringBoot實現配置管理的開發(fā)實踐

    Nacos與SpringBoot實現配置管理的開發(fā)實踐

    在微服務架構中,配置管理是一個核心組件,而Nacos為此提供了一個強大的解決方案,本文主要介紹了Nacos與SpringBoot實現配置管理的開發(fā)實踐,具有一定的參考價值
    2023-08-08
  • Spring Boot啟動時調用自己的非web邏輯

    Spring Boot啟動時調用自己的非web邏輯

    在spring Boot中,有些代碼是WEB功能,例如API等,但是有些邏輯是非WEB,啟動時就要調用并持續(xù)運行的,該如何加載自己的非WEB邏輯呢,下面通過實例代碼給大家講解,一起看看吧
    2017-07-07
  • 深入淺出分析Java 類和對象

    深入淺出分析Java 類和對象

    面向對象乃是Java語言的核心,是程序設計的思想。Java語言的面向對象技術包括了面向對象和面向過程的基本概念,面向對象的特征,Java語言的類,對象,修飾符,抽象類等一系列的知識點
    2022-03-03

最新評論

收藏| 张家港市| 东平县| 尼木县| 乌海市| 方正县| 松溪县| 富源县| 越西县| 拉孜县| 武安市| 搜索| 晋州市| 云林县| 体育| 新和县| 神木县| 赤水市| 游戏| 手机| 汉阴县| 潜江市| 南丰县| 萍乡市| 灯塔市| 布尔津县| 晋城| 镇宁| 肇庆市| 靖宇县| 曲水县| 繁昌县| 彭泽县| 碌曲县| 鄄城县| 武定县| 滦平县| 绍兴市| 楚雄市| 历史| 新民市|