【问题标题】:How do these Java examples work? (existence and bubble sort)这些 Java 示例是如何工作的? (存在与冒泡排序)
【发布时间】:2018-01-18 13:26:12
【问题描述】:
  static void eldontes() {
    System.out.println("--- Choosing ---");
    int[] sorozat = new int[]{1, -1, 3, 5};
    boolean exists = false;
    for (int i = 0; i < sorozat.length && !exists; i++) {
        int elem = sorozat[i];
        if (elem < 0) {
            exists = true;
        }
    }

    System.out.println("There's negative: " + exists);

首先,for 循环中的 !exists 是做什么的? !exist 应该意味着布尔值现在为真,不是吗?所以增加 i utnil sorozat.length AND exists = true ?什么意思?

下面是这个例子:

static void buborekRendezes() {
    System.out.println("--- Bubble order ---");
    int[] sorozat = new int[]{9, 4, 6, 2, 3, 0, 5, 7, 8, 1};
    for (int i = sorozat.length - 1; i > 0; i--) {
        for (int j = 0; j < i; j++) {
            if (sorozat[j] > sorozat[j + 1]) {
                int tmp = sorozat[j];
                sorozat[j] = sorozat[j + 1];
                sorozat[j + 1] = tmp;
            }
        }
    }
    System.out.println(Arrays.toString(sorozat));
}

int tmp = sorozat[j];开始我真的不知道发生了什么。最后三行是做什么的?为什么tmp在代码的末尾?

【问题讨论】:

    标签: java for-loop boolean conditional-statements


    【解决方案1】:

    首先,for循环中的!exists在做什么?

    其实!exists表示布尔变量exists就是false

    !exists   (is equivalent to)    exists == false
    

    for循环的条件块中使用它,只要满足elem&lt;0,即只要exists == true,就退出循环。

    for (int i = 0; i < sorozat.length && !exists; i++) {
        int elem = sorozat[i];
        if (elem < 0) {
            exists = true;
        }
    }
    

    所以增加 i utnil sorozat.length AND exists = true ?什么意思?

    不,这意味着在i&lt;sorozat.lengthexists == false 时递增,只要exists 设置为true,我们就会退出循环。

    【讨论】:

      【解决方案2】:
      int tmp = sorozat[j];
      sorozat[j] = sorozat[j + 1];
      sorozat[j + 1] = tmp;
      

      这是交换两个变量值的基本算法。 基本上,是在过程中使用时间变量 (tmp) 交换值 sorozat[j]sorozat[j+1]。 所以,如果sorozat[j]=4sorozat[j+1]=2在这3行之后,结果将是sorozat[j]=2sorozat[j+1]=4

      【讨论】:

      • 感谢您的快速回答! :)
      【解决方案3】:
      for (int i = 0; i < sorozat.length && !exists; i++) {
              int elem = sorozat[i];
              if (elem < 0) {
                  exists = true;
              }
          }
      

      !exists 是为了确保一旦找到第一个负值,循环就会中断..

      根据您的下一个代码片段

      for (int i = sorozat.length - 1; i > 0; i--) {
              for (int j = 0; j < i; j++) {
                  if (sorozat[j] > sorozat[j + 1]) {
                      int tmp = sorozat[j];
                      sorozat[j] = sorozat[j + 1];
                      sorozat[j + 1] = tmp;
                  }
              }
          }
      

      这是Bubble sort在java中的基本工作代码。先阅读算法,然后更容易理解代码。

      int tmp = sorozat[j];
      sorozat[j] = sorozat[j + 1];
      sorozat[j + 1] = tmp;
      

      这三行是​​swapsorozat[j]sorozat[j+1] 处的值

      【讨论】:

        【解决方案4】:
        1. 直到 i 小于 sorozat.length 并且 ! 存在时才增加 i。术语 !exists 等价于 (exists == false)。因此,当您到达 sorozat 的末尾或找到您要查找的内容(即小于零的元素)时,您的循环将停止。

        2. 这里的值在 sorozat[j] 和 sorozat[j + 1] 之间交换。为此,将 sorozat[j] 值存储在 tmp 变量中以避免被覆盖,然后将 sorozat[j + 1] 放入 sorozat[j] 中,最后从 tmp 中获取的初始 sorozat[j] 值是写入 sorozat[j + 1]。如果没有 tmp 变量,您将失去 sorozat[j] 值。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-09
          • 1970-01-01
          • 1970-01-01
          • 2020-07-26
          • 1970-01-01
          相关资源
          最近更新 更多