简体   繁体   English

如何在阵列中排列从500到1的数组? (JAVA)

[英]How can I arrange an array from 500 down to 1 in an array? (Java)

I'm trying to arrange and printout an array starting at 500 and stopping at 1, I have tried this with the following code, but this will printout starting from 1 and going up to 500: 我正在尝试安排并打印出一个从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));

However using the following code, the array will printout the correct way, but I'm trying to arrange the array fully before printing out values: 但是使用下面的代码,数组将以正确的方式打印出来,但我正在尝试在打印输出值之前完全排列数组:

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

In your first loop, you're using teller3 as both an index and a value. 在第一个循环中,您使用teller3作为索引值。 That means that index 500 will have the value 500, which is the opposite of what you want. 这意味着索引500将具有值500,这与您想要的相反。

You're also making a bit of confusion with the array size. 你也对数组大小有点混淆。

The correct way to do this would be to either do the loop straight and subtract the value from 500 执行此操作的正确方法是直接执行循环并从500中减去该值

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

Or do the same to the array index 或者对数组索引执行相同操作

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

Try: 尝试:

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

The problem here is that you are filling the array backwards. 这里的问题是你向后填充数组。 Starting from the most significant index down to index 1. In order to achieve the result you should fill from index 1 to the most significant one. 从最重要的索引到索引1.为了实现结果,您应该从索引1填充到最重要的索引。

Introducing another variable like index = 1 and using it in such manner will fix the issue: 引入另一个变量如index = 1并以这种方式使用它将解决问题:

int [] aftel = new int [501];
int index = 1;

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

and alternatively the one liner for loop: 或者是一个用于循环的衬里:

int [] aftel = new int [501];

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

System.out.println(Arrays.toString(aftel));

The problem is that you set 500 on position 500, but you must set 500 on position 0. This should work: 问题是您在位置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));

try like this. 试试这样。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM