How to return a specific element of an array?

前端 未结 4 1826
臣服心动
臣服心动 2020-12-20 01:49

I want to return odd numbers of an array yet Eclipse doesn\'t seem to accept my return array[i]; code. I think it requires returning a whole array since I set a

4条回答
  •  心在旅途
    2020-12-20 02:20

    Make sure return type of you method is same what you want to return. Eg: `

      public int get(int[] r)
      {
         return r[0];
      }
    

    `

    Note : return type is int, not int[], so it is able to return int.

    In general, prototype can be

    public Type get(Type[] array, int index)
    {
        return array[index];
    }
    

提交回复
热议问题