从指定的起点填充一个数组,增加n个增量

时间:2022-02-15 14:58:40

So let's say I want to count from "start" to "end" and increasing by 1 where start = 3 and end = 6. so the output should be: {3, 4, 5, 6}

所以假设我想从“开始”到“结束”计数,并且在start = 3和end = 6时增加1,所以输出应为:{3,4,5,6}

here's my code so far

到目前为止这是我的代码

int[] myarray = new int[end - start + 1];
int start = 3;
int end = 6;
for (int a = 0; a < myarray.length; a++) {
    for (int i = start; start <= end; i++) {
        myarray[a] = i; 
    }
} 

but the problem is that the nested for loop does not exit so it keeps on overwriting itself and I'm not sure how to make it 'exit'. any ideas? Thanks in advance.

但问题是嵌套的for循环不会退出,所以它继续覆盖自己,我不知道如何让它'退出'。有任何想法吗?提前致谢。

3 个解决方案

#1


1  

your problem lies in the inner for-loop for (int i = start; start <= end; i++) You increase i, but you are checking start<=end. start does not change.

你的问题在于内部for循环(int i = start; start <= end; i ++)你增加了i,但是你正在检查start <= end。开始不改变。

Further more your code would ouput an array with {6,6,6,6} because with the 2nd for loop you update the array always at the same position. You could just do something like this:

更进一步,您的代码将使用{6,6,6,6}输出数组,因为使用第2个for循环,您始终在同一位置更新数组。你可以做这样的事情:

int start = 3;
int end = 6;
int[] myarray = new int[end - start + 1];
for (int a = 0; a < myarray.length; a++) {
    myarray[a] = start + a;
} 

#2


0  

Using java-8 you don't need even 1 loop, all done in a single line.

使用java-8你甚至不需要1个循环,所有这些都在一行中完成。

int start = 3, end = 6;
int[] myarray = IntStream.rangeClosed(start, end).toArray();

#3


0  

the second loop is unnecessary, you can do it like this, also the star and end has to come before myarray

第二个循环是不必要的,你可以这样做,明星和结束也必须在myarray之前

int start = 3;
int end = 6;
int[] myarray = new int[end - start + 1];
for (int a = 0; a < myarray.length; a++) {    
        myarray[a] = start+a; 
    }
System.out.println(Arrays.toString(myarray));

output

[3, 4, 5, 6]

Edit : dont forget to import Arrays class to print arrays

编辑:别忘了导入Arrays类来打印数组

import java.util.*;

#1


1  

your problem lies in the inner for-loop for (int i = start; start <= end; i++) You increase i, but you are checking start<=end. start does not change.

你的问题在于内部for循环(int i = start; start <= end; i ++)你增加了i,但是你正在检查start <= end。开始不改变。

Further more your code would ouput an array with {6,6,6,6} because with the 2nd for loop you update the array always at the same position. You could just do something like this:

更进一步,您的代码将使用{6,6,6,6}输出数组,因为使用第2个for循环,您始终在同一位置更新数组。你可以做这样的事情:

int start = 3;
int end = 6;
int[] myarray = new int[end - start + 1];
for (int a = 0; a < myarray.length; a++) {
    myarray[a] = start + a;
} 

#2


0  

Using java-8 you don't need even 1 loop, all done in a single line.

使用java-8你甚至不需要1个循环,所有这些都在一行中完成。

int start = 3, end = 6;
int[] myarray = IntStream.rangeClosed(start, end).toArray();

#3


0  

the second loop is unnecessary, you can do it like this, also the star and end has to come before myarray

第二个循环是不必要的,你可以这样做,明星和结束也必须在myarray之前

int start = 3;
int end = 6;
int[] myarray = new int[end - start + 1];
for (int a = 0; a < myarray.length; a++) {    
        myarray[a] = start+a; 
    }
System.out.println(Arrays.toString(myarray));

output

[3, 4, 5, 6]

Edit : dont forget to import Arrays class to print arrays

编辑:别忘了导入Arrays类来打印数组

import java.util.*;