Ad
Code
Diff
  • import java.util.stream.IntStream;
    
    public class Area {
    
      public static long[] workOutArea(final long[] values){
        return values.length % 2 != 0? null: IntStream.range(0, values.length).filter(x -> x % 2 == 0).mapToLong(x -> values[x] * values[x+1]).toArray();
      }
    
    }
    • import java.util.stream.IntStream;
    • public class Area {
    • public static long[] workOutArea(final long[] values){
    • if (values.length % 2 != 0) {
    • return null; // This is ugly!
    • } else {
    • final long[] areas = new long[values.length / 2];
    • for (int i = 0; i < values.length; i += 2) {
    • areas[i / 2] = values[i] * values[i + 1];
    • }
    • return areas;
    • }
    • return values.length % 2 != 0? null: IntStream.range(0, values.length).filter(x -> x % 2 == 0).mapToLong(x -> values[x] * values[x+1]).toArray();
    • }
    • }