在没有设置的情况下从数组中删除重复项

时间:2013-11-21 00:06:10

标签: java

                                  EDITED VERSION

我需要从项目的数组中删除重复项。我已经看到人们建议使用“set”,我没有在课堂上学到这一点,所以我不能使用它。我问过我的导师,他指出了我的方向。这很长,但这只是因为我使用了许多打印语句来帮助我理解代码正在做什么。 (底部代码)。我相信在数组中插入非重复数字会有问题。

                               HERE IS MY OUTPUT
LB: 0
UB: 10
PROBE: 5
Value of Arrays: 0
randomNumber:42
LB: 6
UB: 10
PROBE: 8
Value of Arrays: 0
randomNumber:42
LB: 9
UB: 10
PROBE: 9
Value of Arrays: 0
randomNumber:42
LB: 10
UB: 10
PROBE: 10
Value of Arrays: 0
randomNumber:42
Return False
42
0
0
0
0
0
0
0
0
0
LB: 0
UB: 10
PROBE: 5
Value of Arrays: 42
randomNumber:75
LB: 6
UB: 10
PROBE: 8
Value of Arrays: 42
randomNumber:75
LB: 9
UB: 10
PROBE: 9
Value of Arrays: 42
randomNumber:75
LB: 10
UB: 10
PROBE: 10
Value of Arrays: 0
randomNumber:75
Return False
42
75
42
42
42
42
42
42
42
42
LB: 0
UB: 10
PROBE: 5
Value of Arrays: 75
randomNumber:74
LB: 0
UB: 4
PROBE: 2
Value of Arrays: 75
randomNumber:74
LB: 0
UB: 1
PROBE: 0
Value of Arrays: 42
randomNumber:74
LB: 1
UB: 1
PROBE: 1
Value of Arrays: 75
randomNumber:74
Return False
42
75
74
75
75
75
75
75
75
75
LB: 0
UB: 10
PROBE: 5
Value of Arrays: 75
randomNumber:100
LB: 6
UB: 10
PROBE: 8
Value of Arrays: 75
randomNumber:100
LB: 9
UB: 10
PROBE: 9
Value of Arrays: 75
randomNumber:100
LB: 10
UB: 10
PROBE: 10
Value of Arrays: 0
randomNumber:100
Return False
42
75
75
100
75
75
75
75
75
75
LB: 0
UB: 10
PROBE: 5
Value of Arrays: 100
randomNumber:68
LB: 0
UB: 4
PROBE: 2
Value of Arrays: 75
randomNumber:68
LB: 0
UB: 1
PROBE: 0
Value of Arrays: 42
randomNumber:68
LB: 1
UB: 1
PROBE: 1
Value of Arrays: 75
randomNumber:68
Return False
42
75
75
100
68
100
100
100
100
100
LB: 0
UB: 10
PROBE: 5
Value of Arrays: 100
randomNumber:7
LB: 0
UB: 4
PROBE: 2
Value of Arrays: 75
randomNumber:7
LB: 0
UB: 1
PROBE: 0
Value of Arrays: 42
randomNumber:7
Return False
42
75
75
75
100
7
100
100
100
100
LB: 0
UB: 10
PROBE: 5
Value of Arrays: 100
randomNumber:29
LB: 0
UB: 4
PROBE: 2
Value of Arrays: 75
randomNumber:29
LB: 0
UB: 1
PROBE: 0
Value of Arrays: 42
randomNumber:29
Return False
42
42
75
75
75
100
29
100
100
100
LB: 0
UB: 10
PROBE: 5
Value of Arrays: 75
randomNumber:39
LB: 0
UB: 4
PROBE: 2
Value of Arrays: 42
randomNumber:39
LB: 0
UB: 1
PROBE: 0
Value of Arrays: 42
randomNumber:39
Return False
42
42
42
75
75
75
100
39
100
100
LB: 0
UB: 10
PROBE: 5
Value of Arrays: 75
randomNumber:74
LB: 0
UB: 4
PROBE: 2
Value of Arrays: 42
randomNumber:74
LB: 3
UB: 4
PROBE: 3
Value of Arrays: 42
randomNumber:74
LB: 4
UB: 4
PROBE: 4
Value of Arrays: 75
randomNumber:74
Return False
42
42
42
42
75
75
75
100
74
100
LB: 0
UB: 10
PROBE: 5
Value of Arrays: 75
randomNumber:42
LB: 0
UB: 4
PROBE: 2
Value of Arrays: 42
randomNumber:42
LB: 0
UB: 10
PROBE: 5
Value of Arrays: 75
randomNumber:67
LB: 0
UB: 4
PROBE: 2
Value of Arrays: 42
randomNumber:67
LB: 3
UB: 4
PROBE: 3
Value of Arrays: 42
randomNumber:67
LB: 4
UB: 4
PROBE: 4
Value of Arrays: 75
randomNumber:67
Return False
42
42
42
42
75
75
75
75
100
67
LB: 0
UB: 10
PROBE: 5
Value of Arrays: 75
randomNumber:30
LB: 0
UB: 4
PROBE: 2
Value of Arrays: 42
randomNumber:30
LB: 0
UB: 1
PROBE: 0
Value of Arrays: 42
randomNumber:30
Return False
42
42
42
42
75
75
75
75
75
100
                               HERE IS MY CODE  

 public class BinarySearch3{
    public static boolean binarySearch(int[] Arrays, int randomNumber){
        int LB = 0;//declare the lower bound
        int UB = 10;//declare the upper bound
        int probe = (LB + UB) / 2;//calculate the probe
        while(LB <= UB){
            System.out.println("LB: " + LB);  
            System.out.println("UB: " + UB); 
            System.out.println("PROBE: " + probe); 
            System.out.println("Value of Arrays: " + Arrays[probe]); 
            System.out.println("randomNumber:" + randomNumber);  
            //if the number is found return true
            if(Arrays[probe] == randomNumber)
                return true;
            // if the probe is less than the number you want to find make LB
            // the probe + 1. Cutting the list in half
            if(Arrays[probe] < randomNumber)
                LB = probe + 1;
            // if the probe is more than the number you want to find make UB
            // the probe - 1. Cutting the list in half
            else if(Arrays[probe] > randomNumber)
                UB = probe - 1;
            probe = (LB + UB) / 2;//recalculate probe
        }
        // the number was not found
        System.out.println("Return False");
        return false;
    }

    public static void main(String [] args){
        int Arrays[] = new int [11];
        //Check the array
        int randomNumber = 0; int d=0 ;
        while(d < Arrays.length){
            sort.sorting(Arrays);
            do{ // loop until randomNumber not found in array

                randomNumber = (int) (Math.random() * 100) + 1;
            } while(binarySearch(Arrays, randomNumber) == true);
            Arrays[d] = randomNumber; 
            d++;
            for(int k = 0; k < Arrays.length-1; k++) {
                System.out.println(Arrays[k]);
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

一个等号是指定一个值。

if(dup = false)应为if(!dup)if(dup == false)

代码中的另一件事是您创建变量boolean test = true; 你永远不会改变它。因此,在第二次迭代中,它将不执行任何操作,因为您的while语句为while(!test)

为了正确执行,您必须稍微改进一下代码。

答案 1 :(得分:1)

我建议让二进制搜索它自己的函数,这将从二进制搜索中分离出数组的构建。这使得可读性和测试更加容易,二进制搜索可以重复使用。

public class work_on_it{
    public static boolean binarySearch(int[] array, int z){
        int LB = 0;//declare the lower bound
        int UB = array.length - 1;//declare the upper bound
        int probe = (LB + UB) / 2;//calculate the probe
        while(LB <= UB){
            //if the number is found return true
            if(array[probe] == z)
                return true;
            // if the probe is less than the number you want to find make LB
            // the probe + 1. Cutting the list in half
            if(array[probe] < z)
                LB = probe + 1;
            // if the probe is more than the number you want to find make UB
            // the probe - 1. Cutting the list in half
            else if(array[probe] > z)
                UB = probe - 1;
            probe = (LB + UB) / 2;//recalculate probe
        }
        // the number was not found
        return false;
    }

    public static void main(String [] args){
        // array length = LENGTH, array values in (1,...,RANGE)
        int LENGTH = 11, RANGE = 50;
        int[] array = new int [LENGTH];
        // fill array with MAX_VALUE. Ensures unassigned elements last after sort.
        Arrays.fill(array, Integer.MAX_VALUE);

        int z = 0, x = 0;
        while(x < array.length){
            Arrays.sort(array);
            do{ // loop until z not found in array
                z = (int) (Math.random() * RANGE) + 1;
            } while(binarySearch(array, z) == true);
            array[x++] = z;  
        }

        for(int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}  

答案 2 :(得分:0)

我没有完全通过代码逻辑,但是我发现了一些错误,

1。     if(dup = true)     {        z =(int)(Math.random()* 10)+ 1;     }
    其他     if(dup = false)     {                     array [x] = z;
    }

在这里,您要分配值而不是比较,使用'=='进行比较。

2。     而(!测试) 在这里,因为test之前设置为true,所以它将永远不会进入,因为条件将被评估为false