import java.util.ArrayList;
import java.util.Arrays;
public class DivisiveArrayOfNumbers {
public int[] solution(int divisor, int[] arr) {
int[] answer;
ArrayList<Integer> a1 = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i ) {
if (arr[i] % divisor == 0)
a1.get(i);
}
if (a1.isEmpty()) {
a1.add(-1);
}
answer = new int[a1.size()];
for (int i = 0; i < a1.size(); i ) {
answer[i] = a1.get(i);
}
Arrays.sort(answer);
return answer;
}
public static void main (String[] args) {
DivisiveArrayOfNumbers method = new DivisiveArrayOfNumbers();
System.out.println(method.solution(5, new int{}{5, 9, 7, 10, 5}));
}
}
I want to put it together int, int[] in "method.solution( )".
but the following error appears.
I want to make it recognized as int[] type, is there a way?
I saw the comments and corrected them :)
CodePudding user response:
Try:
System.out.println(Arrays.ToString(method.solution(5, new int[]{9, 7, 10, 5})));
CodePudding user response:
You can achieve this by
public int[] solution(int divisor, int[] arr)
{
System.out.println(Arrays.toString(arr));
return arr;
}
public static void main (String[] args) {
Index method = new Index();
System.out.println(method.solution(5, new int []{9, 7, 10,5}));
}
output
[9, 7, 10, 5]
[I@15db9742
CodePudding user response:
The issue you are having is that you did not pass the right number of arguments.
As you can see from the first line,
solution(int divisor, int[] arr)
has two arguments. You know this because there is one comma that separates thedivisor
parameter from thearr
parameter.You attempted to call
method.solution(5, 9, 7, 10,5)
. As you can see, there are 5 arguments separated by 4 commas.What you want is to group the last 4 arguments together into an array as evident by the type of the second argument:
int[]
This is the shorthand way of making an array
new int[]{9, 7, 10, 5}
You may be more familiar with
int[] myArray = {9, 7, 10, 5}
You can call the function in one of two ways:
int[] myArray = {9, 7, 10, 5};
System.out.println(method.solution(5, myArray));
or
System.out.println(method.solution(5, new int []{9, 7, 10,5}));