2013-02-16 50 views
0

無法編譯此編程4搜索數組中的數字.. .class在編程的最後一行預期錯誤:obj.searchnumber(int arr1 [],item);.class對於我的程序的預期錯誤

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

您不會在方法調用中像那樣傳遞數組。你只需要使用名稱的數組是這樣的:

obj.searchnumber(arr1, item); 

int arr1[]形式只是用來在數組的聲明的時間。它只是創建一個新的數組引用,並說arr1類型爲int []。你不能在某些方法調用中使用它。它是引用實際數組的arr1。你只能通過這個名字訪問數組。

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