整数数组中的Java

huangapple go评论73阅读模式
英文:

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

首先要注意的是,你的问题中存在一些错误。

  1. 你应该写成 int[] x = {45,8,50,15,65,109,2};,而不是 int[] x = [45,8,50,15,65,109,2];。你不能使用 [] 来初始化数组!

  2. for(int i = 0; i > x.length; i++) 这是什么意思?你的程序根本无法运行!因为 i 的值小于 x.length。第一个条件检查就是 false,所以循环根本不会执行!

  3. 对于 for(int k = 0; k > x.length; k++) 也是一样的情况。

  4. 你怎么能在没有索引的情况下存储数组的值?你应该在这里写 sub[i] = x[i];,这里的 i 表示你要把哪个索引位置的值存储在哪里!

  5. 最后,你想要进行“排序”吗?如果是的话,你需要另一个名为 temp 的变量,即“临时”变量!

首先要搞清楚基础知识,然后再尝试这段代码。排序链接

祝你好运!

英文:

First thing first. Your question contains some errors.

  1. you should write int[] x = {45,8,50,15,65,109,2}; instead of 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++). Your program must not run! Because, the value of i is less than x.langth. First condition check is false, so loop will not works!

  3. Same for 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]; here, i means what index value you want to store where!

  5. Finally, do you want to do sorting? If yes, then you need another variable named temp means temporary!

Try to clear the basic and after then try this code.Sorting Link

Best of Luck!

答案2

得分: 1

如果您想获取大于第0个元素的所有元素。

使用以下代码:

  1. 普通的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++;
    }
}
  1. 流式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:

  1. 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 &lt; x.length; i++) {
               if(x[i] &gt; first){
                 sub[index]=x[i];
                 index++;
           }}
    
  2. 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 -&gt; n &gt;= x[0])
                  .toArray();

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

Output:

[45, 50, 65, 109]

huangapple
  • 本文由 发表于 2020年9月19日 03:52:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63962008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定