英文:
Integers in array Java
问题
我有一个包含[45,8,50,15,65,109,2]的数组。我想检查一下数组的第一个元素是否大于我的第一个元素。
如果 (45 > 8),那么我想把45添加到全新的子数组中。
我想继续检查我的数组,看看是否有任何大于45的数,并将它们添加到新的子数组中。
新的子数组必须保持以添加它们的方式添加的数字。结果应为[45,50,65,109]
我以为通过创建这个是朝着正确的方向前进的,但我做错了什么,所以请帮忙。
int[] x = [45,8,50,15,65,109,2];
int[] sub = null;
for(int i = 0; i > x.length; i++) {
for(int k = 0; k > x.length; k++) {
if(x[i] > x[k]){
sub = i;
英文:
I have an array that contains [45,8,50,15,65,109,2]. i would like to check to see if the zero element of my array is greater than my first element.
if ( 45 > 8)
then i would like to add 45 to brand new sub array.
i would like to continue checking my array for anything that is bigger than 45 and add it to the new sub array.
The new sub array has to keep the numbers added in the way they where added.The result should be [45,50,65,109]
I thought by creating this was going in the right direction but im doing something wrong so please help.
int[] x = [45,8,50,15,65,109,2];
int[] sub = null;
for(int i = 0; i > x.length; i++) {
for(int k = 0; k > x.length; k++) {
if(x[i] > x[k]){
sub = i;
答案1
得分: 2
首先要注意的是,你的问题中存在一些错误。
-
你应该写成
int[] x = {45,8,50,15,65,109,2};
,而不是int[] x = [45,8,50,15,65,109,2];
。你不能使用[]
来初始化数组! -
for(int i = 0; i > x.length; i++)
这是什么意思?你的程序根本无法运行!因为i
的值小于x.length
。第一个条件检查就是 false,所以循环根本不会执行! -
对于
for(int k = 0; k > x.length; k++)
也是一样的情况。 -
你怎么能在没有索引的情况下存储数组的值?你应该在这里写
sub[i] = x[i];
,这里的i
表示你要把哪个索引位置的值存储在哪里! -
最后,你想要进行“排序”吗?如果是的话,你需要另一个名为
temp
的变量,即“临时”变量!
首先要搞清楚基础知识,然后再尝试这段代码。排序链接
祝你好运!
英文:
First thing first. Your question contains some errors.
-
you should write
int[] x = {45,8,50,15,65,109,2};
instead ofint[] x = [45,8,50,15,65,109,2];
You can not use[]
to initialize array! -
What does it mean
for(int i = 0; i > x.length; i++)
. Your program must not run! Because, the value ofi
is less thanx.langth
. First condition check is false, so loop will not works! -
Same for
for(int k = 0; k > x.length; k++)
-
How do you want to store value in an array without index? You have to write
sub[i] = x[i];
here,i
means what index value you want to store where! -
Finally, do you want to do
sorting
? If yes, then you need another variable namedtemp
meanstemporary
!
Try to clear the basic and after then try this code.Sorting Link
Best of Luck!
答案2
得分: 1
如果您想获取大于第0个元素的所有元素。
使用以下代码:
- 普通的Java代码:
int[] x = {45, 8, 50, 15, 65, 109, 2};
int[] sub = new int[x.length];
int first = x[0];
sub[0] = first;
int index = 1;
for (int i = 1; i < x.length; i++) {
if (x[i] > first) {
sub[index] = x[i];
index++;
}
}
- 流式API:
int[] x = {45, 8, 50, 15, 65, 109, 2};
int[] sub = Arrays.stream(x).filter(number -> number >= x[0]).toArray();
其中stream()
将数组转换为流,filter
应用所需的条件,然后再将其转换回数组。
英文:
If you're trying to fetch everything greater than 0th element.
Use the following:
-
Plain old java code:
int[] x = {45,8,50,15,65,109,2}; int[] sub = new int[x.length]; int first = x[0]; sub[0]=first; int index=1; for(int i = 1; i < x.length; i++) { if(x[i] > first){ sub[index]=x[i]; index++; }}
-
Streams API:
> int[] x = {45, 8, 50, 15, 65, 109, 2};
>
> int[] sub = Arrays.stream(x).filter(number -> number>=
> x[0]).toArray();
where stream() is converting array to a stream, filter is applying the required condition and then ending it with conversion into an Array again.
答案3
得分: 1
使用Java 8 Stream API 可以对输入数组进行过滤:
int[] x = {45, 8, 50, 15, 65, 109, 2};
int[] sub = Arrays.stream(x)
.filter(n -> n >= x[0])
.toArray();
System.out.println(Arrays.toString(sub));
输出:
[45, 50, 65, 109]
英文:
It is possible to filter the input array using Java 8 Stream API:
int[] x = {45, 8, 50, 15, 65, 109, 2};
int[] sub = Arrays.stream(x)
.filter(n -> n >= x[0])
.toArray();
System.out.println(Arrays.toString(sub));
Output:
[45, 50, 65, 109]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论