【问题标题】:How can I arrange an array from 500 down to 1 in an array? (Java)如何在数组中排列从 500 到 1 的数组? (爪哇)
【发布时间】:2018-10-03 12:10:08
【问题描述】:

我正在尝试排列并打印一个从 500 开始到 1 停止的数组, 我已经用下面的代码试过了,但这将从 1 开始打印到 500:

int [] aftel = new int [501];
for (int teller3 = 500; teller3 > 0; teller3--){
       aftel[teller3] = teller3;
    }
System.out.println(Arrays.toString(aftel));

但是使用下面的代码,数组会以正确的方式打印出来,但我试图在打印出值之前完全排列数组:

int [] aftel = new int [501];
for (int teller2 = 1; teller2 <= 500; teller2++){
        optel500[teller2] = teller2;
        System.out.print(optel500[teller2]+" ");
    }

【问题讨论】:

    标签: java arrays sorting


    【解决方案1】:

    在您的第一个循环中,您使用teller3 作为索引 和 一个值。这意味着索引 500 的值将是 500,这与您想要的相反。

    您还对数组大小感到困惑。

    执行此操作的正确方法是直接执行循环并从 500 中减去该值

    int [] aftel = new int [500];
    for (int teller3 = 0; teller3 < 500; teller3++){
        aftel500[teller3] = (500 - teller3) + 1;
    }
    

    或者对数组索引做同样的事情

    int [] aftel = new int [500];
    for (int teller3 = 500; teller3 > 0; teller3--){
       aftel[500 - teller3] = teller3;
    }
    

    【讨论】:

      【解决方案2】:

      试试:

      int[] array = IntStream
              .range(0, 500) // get a stream of [0, 499]
              .map(i -> 500 - i) // map it to [500, 1]
              .toArray(); 
      

      【讨论】:

        【解决方案3】:

        问题是你在位置 500 上设置了 500,但是你必须在位置 0 上设置 500。 这应该有效:

        int [] aftel = new int [501];
        for (int teller3 = 500; teller3 > 0; teller3--){
               aftel[500 - teller3] = teller3;
            }
        System.out.println(Arrays.toString(aftel));
        

        【讨论】:

          【解决方案4】:

          这里的问题是你正在向后填充数组。从最重要的索引开始向下到索引 1。为了获得结果,您应该从索引 1 填充到最重要的索引。

          引入另一个变量,如index = 1 并以这种方式使用它可以解决问题:

          int [] aftel = new int [501];
          int index = 1;
          
          for (int teller3 = 500; teller3 > 0; teller3--){
              aftel[index] = teller3;
              index++;
          }
          

          或者一个循环的衬里:

          int [] aftel = new int [501];
          
          for (int teller3 = 500, index = 1; teller3 > 0; teller3--, index++){
              aftel[index] = teller3;
          }
          
          System.out.println(Arrays.toString(aftel));
          

          【讨论】:

          • Java 是基于 0 的。他不应该从索引 1 开始,他应该从索引 0 开始
          • 首先检查 OPs 要求。他想填充封闭的索引范围 [1, 500]。
          • 他说的是 1 到 500 的值。至于他使用这些索引的代码,你有没有想过他没有意识到 Java 是基于 0 的?帖子中没有提到指数应该是 1 到 500
          • 在最初的数组类中,老师的建议是使用 N+1 大小的数组,然后在 [1, N] 中使用索引。因此,从这种角度来看,这是一个有效的案例。引用代码中使用的逻辑非常接近,所以我猜他是数组领域的新手。他还在输出的开头看到了额外的数字(0),但没有提到这是一个问题,而不是元素的顺序。
          • 将数组理解为数据结构更容易一些。几节课之后,他们将让您使用索引并测试您对基于 0 的数组的理解(不仅是 java -> 它确实是一个 C 类)。这也是在大学里,而我从高中班就很了解数组,这对我来说也很奇怪。
          【解决方案5】:

          像这样试试。

          int noOfElements = 500;
          int[] array = new int[noOfElements];
          
          for (int i = 0; i < noOfElements; i++){
              array[i] = noOfElements - i;
          }
          
          for(int a : array){
              System.out.println(a);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-09-03
            • 1970-01-01
            • 2011-01-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-08-20
            • 1970-01-01
            相关资源
            最近更新 更多