简体   繁体   English

Java数组中的整数

[英]Integers in array Java

I have an array that contains [45,8,50,15,65,109,2].我有一个包含 [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.如果 ( 45 > 8) 那么我想将 45 添加到全新的子数组中。
i would like to continue checking my array for anything that is bigger than 45 and add it to the new sub array.我想继续检查我的数组是否大于 45 并将其添加到新的子数组中。 The new sub array has to keep the numbers added in the way they where added.The result should be [45,50,65,109]新的子数组必须以添加的方式保留添加的数字。结果应该是 [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;

First thing first.先说第一件事。 Your question contains some errors.您的问题包含一些错误。

  1. you should write int[] x = {45,8,50,15,65,109,2};你应该写int[] x = {45,8,50,15,65,109,2}; instead of int[] x = [45,8,50,15,65,109,2];而不是int[] x = [45,8,50,15,65,109,2]; You can not use [] to initialize array!你不能用[]来初始化数组!

  2. What does it mean for(int i = 0; i > x.length; i++) . for(int i = 0; i > x.length; i++)是什么意思。 Your program must not run!你的程序不能运行! Because, the value of i is less than x.langth .因为, i的值小于x.langth First condition check is false, so loop will not works!第一个条件检查是假的,所以循环不起作用!

  3. Same for for(int k = 0; k > x.length; k++) for(int k = 0; k > x.length; k++)

  4. How do you want to store value in an array without index?你想如何在没有索引的数组中存储值? You have to write sub[i] = x[i];你必须写sub[i] = x[i]; here, i means what index value you want to store where!在这里, i意思是你想在哪里存储什么索引值!

  5. Finally, do you want to do sorting ?最后,你想做sorting吗? If yes, then you need another variable named temp means temporary !如果是,那么您需要另一个名为temp变量表示temporary

Try to clear the basic and after then try this code.尝试清除基本,然后尝试此代码。 Sorting Link 排序链接

Best of Luck!祝你好运!

If you're trying to fetch everything greater than 0th element.如果您试图获取大于第 0 个元素的所有内容。

Use the following:使用以下内容:

  1. Plain old java code:普通的旧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++; }}
  2. Streams API:流API:

int[] x = {45, 8, 50, 15, 65, 109, 2}; int[] x = {45, 8, 50, 15, 65, 109, 2};

int[] sub = Arrays.stream(x).filter(number -> number>= x[0]).toArray(); 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.其中 stream() 正在将数组转换为流,过滤器正在应用所需的条件,然后再次转换为数组来结束它。

It is possible to filter the input array using Java 8 Stream API:可以使用 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]

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

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