How to pass an array to the argument in java

Viewed 27

When i execute the code it invokes this error

Error: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from int to int[]

import java.util.Arrays;

public class Two_Sum {
  public int Two_Sum(int[] Array,int Target){
    for(int i=0;i<Array.length;i++){
        for(int j=i+1;j<Array.length;j++){
            if(Array[i]+Array[j]==Target){
                return new int[] {i,j};
            }
        }
    }
    throw new IllegalArgumentException("Target number is not found in sum of array")
  }
  public static void main(String[] args) {
    Two_Sum ts=new Two_Sum();
    int[] intArray={1,4,6,8};
    int[] result=ts.Two_Sum(intArray,5);
    System.out.println(Arrays.toString(result));
0 Answers
Related