How to return a specific element of an array?

前端 未结 4 1825
臣服心动
臣服心动 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:44

    I want to return odd numbers of an array

    If i read that correctly, you want something like this?

    List getOddNumbers(int[] integers) {
      List oddNumbers = new ArrayList();
      for (int i : integers)
        if (i % 2 != 0)
          oddNumbers.add(i);
      return oddNumbers;
    }
    

提交回复
热议问题