日期:2014-05-20  浏览次数:20941 次

为什么我定义了还是错误啊?
public class Test_search 
{
public static void main(String[] args) 
{
int[] a={1,3,6,8,9,10,12,18,20,34};
int i = 12;
System.out.println(binarySearch(a,i));
}

  public static int search(int[] a, int num)
  {
for(int i=0;i<a.length;i++)
{
if(a[i]==num)
{
return i;
}
return -1;
}
public static int binarySearch(int[] a,int num)
{
if(a.length==0) 
{
return -1;
}
 
int startPos = 0;
int endPos = a.length-1;
int m = (startPos + endPos)/2;
while(startPos<=endPos)
{
if(num == a[m]) return m;
if(num > a[m])
{
startPos = m+1;
}
if(num < a[m])
{
endPos = m-1;
}
m=(startPos + endPos)/2;
}
}
  } 
}
显示的错误是这样的:Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method binarySearch(int[], int) is undefined for the type Test_search

at Test_search.main(Test_search.java:8)
这个错误是不是说我定义方法错了?但是我不知道怎么改啊。。。。

------解决方案--------------------
lz要记得 每帖必结,提高结帖率
Java code


public class Test_search
{
    public static void main(String[] args)
    {
        int[] a={1,3,6,8,9,10,12,18,20,34};
        int i = 12;
        System.out.println(binarySearch(a,i));
    }

    public static int search(int[] a, int num)
    {
        for(int i=0;i<a.length;i++)
        {
            if(a[i]==num)
            {
                return i;
            }

        }
               return -1;
    }
    public static int binarySearch(int[] a,int num)
    {
        if(a.length==0)
        {
            return -1;
        }

        int startPos = 0;
        int endPos = a.length-1;
        int m = (startPos + endPos)/2;
        while(startPos<=endPos)
        {
            if(num == a[m])
                return m;
            if(num > a[m])
            {
                startPos = m+1;
            }
            if(num < a[m])
            {
                endPos = m-1;
            }
            m=(startPos + endPos)/2;
        }
        return -1;
    }
}

------解决方案--------------------
Java code

public class Test_search {
    public static void main(String[] args) {
    int[] a = { 1, 3, 6, 8, 9, 10, 12, 18, 20, 34 };
    for(int i=0; i<13; i++)
    System.out.println(search(a,i) + "      "  + binarySearch(a, i));
    }

    public static int search(int[] a, int num) {
    for (int i = 0; i < a.length; i++) {
        if (a[i] == num) {
        return i;
        }
        //return -1;把这一行代码移至方法最后return -1;
    }

    return -1;// 没有返回
    }

    public static int binarySearch(int[] a, int num) {
    if (a.length == 0) {
        return -1;
    }

    int startPos = 0;
    int endPos = a.length - 1;
    int m = (startPos + endPos) / 2;
    while (startPos <= endPos) {
        if (num == a[m])
        return m;
        if (num > a[m]) {
        startPos = m + 1;
        }
        if (num < a[m]) {
        endPos = m - 1;
        }
        m = (startPos + endPos) / 2;
    }
    return -1;//没有返回
    }
}
/*
-1      -1
0      0
-1      -1
1      1
-1      -1
-1      -1
2      2
-1      -1
3      3
4      4
5      5
-1      -1
6      6
 */