0

I seen this question on Codility. write a function:

class solution{ public int[] solution(int X, int Y);}

that given X=0 and Y=100, return an array of integers that are odd between 0 and 100.

I wrote the following code:

import java.util.*;

public class Solution{
    int X = 0;
    int Y = 10;
    ArrayList<Integer> arrli = new ArrayList<Integer>();
    public int[] solution( int X, int Y){
        for(int i = X; i<=Y;i++){
            if(i % 2 != 0){
               arrli.add(i);
            }
        }return (int[])arrli.toArray();
    }
}

And got the below error:

error: incompatible types: Object[] cannot be converted to int[]
    }return (int[])arrli.toArray();
Ivar
  • 6,138
  • 12
  • 49
  • 61
  • 1
    @softwareelceng So you seem to be struggling in how to convert your `ArrayList` to an `int[]`. In that case [How to convert an ArrayList containing Integers to primitive int array?](https://stackoverflow.com/questions/718554/how-to-convert-an-arraylist-containing-integers-to-primitive-int-array) has multiple answers showing different ways of doing so. – OH GOD SPIDERS Jul 14 '20 at 13:40
  • has anyone any better way of completing the above question of returning an array of integers that are odd between 0 and 100. – softwareelceng Jul 14 '20 at 13:51
  • sure. start with the first odd one, and just add 2 each iteration, instead of checking the rest of division – Stultuske Jul 14 '20 at 13:53

3 Answers3

1

You should not use class for this solution you can just use a method to create this list.

If you are using java 8 and newer versions you can use

public ArrayList<Integer> createIntegerRangeWithJumps(Integer start, Integer jumpIn , Integer size){
return IntStream.iterate(start , n -> n + jumpIn).limit(size).toList();
}

The call createIntegerRangeWithJumps(1,2,50) will preduce the wanted list

Itay wazana
  • 229
  • 2
  • 9
0

Your title is misleading. Your code looks correct in creating an array of odd numbers, but there are shorter codes.

With respect to the error:

error: incompatible types: Object[] cannot be converted to int[]
    }return (int[])arrli.toArray();

which I assume is you primary question:

You are creating an array of Integer objects, not of int. You can convert your Integer[] to int[]. But an easier approach is to create an array of int[] from the beginning. For the conversion see: How to convert Integer[] to int[] array in Java?

Christian Fries
  • 16,175
  • 10
  • 56
  • 67
  • This should be a comment. – EJoshuaS - Stand with Ukraine Jul 14 '20 at 14:32
  • now getting errors on my return statement. import java.util.*; public class Solution{ int X = 0; int Y = 10; ArrayList arrli = new ArrayList<>(); public int[] solution( int X, int Y){ for(int i = X; i<=Y;i++){ if(i % 2 != 0){ arrli.add(i); } int[] arr = new int[arrli.size()]; for(int t = 0; i < arrli.size(); t++) { arr[t] = arrli.get(t); } } } } return arr; Error: Solution.java:20: error: class, interface, or enum expected return arr; – softwareelceng Jul 14 '20 at 15:06
  • Check the number of opening and closing brackets. If you are using Eclipse use Command-A Command-I (source->Correct indentation) to check your code. return should be inside the function. It is outside the class! – Christian Fries Jul 14 '20 at 15:12
  • @ChristianFries I am using a free online editor, hence i I am unable to use these tools. where do you suggest i move the return statement to. – softwareelceng Jul 14 '20 at 15:17
0

Another solution using Java 8 (assuming X and Y to be inclusive)

int[] oddNums = IntStream.range(X, Y+1).filter(i -> i%2 != 0).toArray();