首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >编写一个返回新数组的程序,该数组包含所有大于java中数组第一个值的值。

编写一个返回新数组的程序,该数组包含所有大于java中数组第一个值的值。
EN

Stack Overflow用户
提问于 2020-01-16 09:45:15
回答 1查看 322关注 0票数 1

编写一个程序,返回一个新数组,该数组包含所有大于数组中第一个值的值。如果数组中没有大于第一个值的值,则返回空数组。如果数组为空,则返回空数组。

我这样解决这个问题:

代码语言:javascript
运行
复制
public class RayGetFirst
{
    //method go will return an array
    //containing all values > the first value in the array
    //from the array parameter ray
    public static int[] go(int[] ray)
    {
        int first = ray[0];
    int[] result = new int[];
    for( int i = 1; i<ray.length; i++)
    {
      if(first < ray)
      {
        return result;
      }
    }
     return result;
  }
}

首先,我分离了数组的第一个索引,然后在将第一个数组与数组的其余部分进行比较之后,为1个索引启动了for循环。但我并没有以正确的答案结束。有没有人能用我正在使用的相同逻辑来纠正这个问题?

此程序的Runner:

代码语言:javascript
运行
复制
import java.util.*;
class Main 
{
  public static void main(String[] args)
  {
    RayGetFirst rt = new RayGetFirst();
    System.out.println( rt.go( new int[]{-99,1,2,3,4,5,6,7,8,9,10,5} ) );
        System.out.println( rt.go( new int[]{10,9,8,7,6,5,4,3,2,1,-99} ) );
        System.out.println( rt.go( new int[]{10,20,30,40,50,-11818,40,30,20,10} ) );
        System.out.println( rt.go( new int[]{32767} ) );
        System.out.println( rt.go( new int[]{255,255} ) );
        System.out.println( rt.go( new int[]{9,10,-88,100,-555,2} ) );
        System.out.println( rt.go( new int[]{10,10,10,11,456} ) );
        System.out.println( rt.go( new int[]{-111,1,2,3,9,11,20,1} ) );
        System.out.println( rt.go( new int[]{9,8,7,6,5,4,3,2,0,-2,6} ) );
        System.out.println( rt.go( new int[]{12,15,18,21,23,1000} ) );
        System.out.println( rt.go( new int[]{250,19,17,15,13,11,10,9,6,3,2,1,0} ) );    
        System.out.println( rt.go( new int[]{9,10,-8,10000,-5000,-3000} ) );
  }
} 

这个跑步者的正确答案是:

代码语言:javascript
运行
复制
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5]
[]
[20, 30, 40, 50, 40, 30, 20]
[]
[]
[10, 100]
[11, 456]
[1, 2, 3, 9, 11, 20, 1]
[]
[15, 18, 21, 23, 1000]
[]
[10, 10000]

谢谢你

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-16 10:07:46

您需要返回一个值>的数组,而不是所提供的数组中的第一个。

代码语言:javascript
运行
复制
public class RayGetFirst {
    //method go will return an array
    //containing all values > the first value in the array
    //from the array parameter ray
    public static int[] go(int[] ray) {
        int first = ray[0];
        int k = 0;                           // set an index
        int[] result = new int[ray.length];  // allocate storage for values.
        for( int i = 1; i<ray.length; i++) {
           if(first < ray[i]) {
              result[k++] = ray[i];     // copy values
           }
        }

    // now establish a new array of length k
        int [] ret = new int[k];
    // and copy the first `k` values in `result` to that array.
    // Then return that array.
        for (int i = 0; i < k; i++) {
            ret[i] = result[i];
        }

        return ret;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59761951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档