.class我的程序的预期错误

时间:2013-02-16 15:09:44

标签: java

无法编译此编程4搜索数组中的数字.. .class最后一行的预期错误:obj.searchnumber(int arr1 [],item);

import java.util.*; 
    public class Ans5
    {
      public void searchnumber(int arr[],int item){
          int low = 0;
          int high = arr.length-1;
          int mid;
          while (low <= high) {
            mid = (low + high) / 2;
            if (arr[mid] > item)               high = mid - 1;
            else if (arr[mid] < item)          low = mid + 1;
            else                            
                System.out.println("The searched item"+item+"is found in the array");
          }
      }

      public static void main(String args[]) {
          Ans5 obj= new Ans5();
          Scanner sc=new Scanner(System.in);
          int arr1[]={1,2,3,4,5,6,7,8,9};
          System.out.print("\fEnter the item u need to search: ");
          int item=3;//sc.next();
          obj.searchnumber(int arr1[],item); // here's the error shown at arr1[]
      }
    }

2 个答案:

答案 0 :(得分:2)

在方法调用中不传递类似的数组。您只需要使用这样的数组名称:

 obj.searchnumber(arr1, item);

int arr1[]表单仅在声明数组时使用。它只是创建一个新的数组引用,并说arr1的类型为int []。您不能在某些方法调用中使用它。它是引用实际数组的arr1。并且您只能通过此名称访问阵列。

答案 1 :(得分:1)

obj.searchnumber(int arr1[],item);
                  ^^^^^^^^^________________Can't declare a variable here. if ypu want to pass the array then simply name it as @Rohit said