Home > database >  How should I pass a list as a value of the functions argument [duplicate]
How should I pass a list as a value of the functions argument [duplicate]

Time:09-29

I have a function where it wants a list

public static int binarySearch(int[] A)

WHAT I'VE TRIED:

new ArrayList<Integer>();
asList = Arrays.asList(-1, 0, 1, 2, 3, 4, 7, 9, 10, 20);
int result = binarySearch(asList);

WHATS THE PROBLEM:

  • Error: "The method binarySearch(int[] A) in the type DecimalToBinary is not applicable for the arguments (List)"

  • I don't know how else I'm going to pass a list in these function.

CodePudding user response:

You can pass it just as an array:

int result = binarySearch(new int[] {-1, 0, 1, 2, 3, 4, 7, 9, 10, 20});

CodePudding user response:

your method expects an Array of integers but you are passing an ArrayList of integers... try changing the method signature:

public static int binarySearch(List<Integer> A)
  •  Tags:  
  • java
  • Related