Java - 传递一个数组,但它正在丢弃元素

时间:2016-10-05 15:58:12

标签: java arrays

private static void split(int arr[], int low, int high){
    int[] newArr = arr;
    //Split num of coins into 3 sub arrays
    int mid1 = (int) Math.floor((low+high)/3);
    int mid2 = (int) Math.floor((low+high)/1.5);

    for(int i=0; i<= high; i++){
        System.out.print("newArr:"+newArr[i]+", ");

    }
    System.out.print("mid1:"+mid1+" mid2:"+mid2+"\n");

    int[] a = new int[mid1+1];
    int[] b = new int[mid1+1];
    int[] c = new int[mid1+1];
    int i, j, k;

    for(i=0; i<= mid1; i++){ //1,2,3
        a[i] = arr[i];
        System.out.println("a"+a[i]);
    }
    i=0;
    for(j=mid1+1; j<=mid2; j++){ //4,5,6
        b[i] = arr[j];
        System.out.println("b"+b[i]);
        i++;
    }
    i=0;
    for(k=mid2+1; k<=high; k++){ //7,8,9
        c[i] = arr[k];
        System.out.println("c"+c[i]);
        i++;
    }
    System.out.print("a: ");
    for(i=0;i<a.length;i++){
        System.out.print(a[i]+", ");
    }
    System.out.print("b: ");
    for(i=0;i<b.length;i++){
        System.out.print(b[i]+", ");
    }
    System.out.print("c: ");
    for(i=0;i<c.length;i++){
        System.out.print(c[i]+", ");
    }
    System.out.println("");

    //Compare
    int sumA=0, sumB=0;
    for(i=0; i<a.length; i++){
        sumA += a[i];
    }
    System.out.println("sumA:"+sumA);
    for(j=0; j<b.length; j++){
        sumB += b[j];
    }
    System.out.println("sumB:"+sumB);

    if(sumA > sumB){
        //check if element is last coin in arr
        if(a.length == 1){
            System.out.print("You've found the coin in a!");
        } else {
            //split a
            System.out.println("split a: ");
            split(a, a[0], a[a.length - 1]);
        }
    }
    else if(sumB > sumA ){
        //check if element is last coin in arr
        if(b.length == 1){
            System.out.print("You've found the coin in b!");
        } else {
            //split b
            System.out.println("split b: ");
            System.out.print("b: ");
            for(i=0;i<b.length;i++){
                System.out.print(b[i]+", ");
            }
            split(b, b[0], b[b.length-1]);
        }
    }
    else{
        //check if element is last coin in arr
        if(c.length == 1){
            System.out.print("You've found the coin in c!");
        } else {
            //split orig last 3
            System.out.println("split c: ");
            split(c, c[0], c[c.length - 1]);
        }
    }

}

我的原始数组= int [] arr = {0,0,0,0,1,0,0,0,0}; 我试图找到价值之一。一旦它通过split函数,值0,1,0将在数组b中,因此它将再次调用split,但是当你在split中检查newArr时,它只有值0而不是0,1 ,0。为什么我的元素消失了。

P.S有很多印刷品陈述,我一直试图弄清楚为什么它没有抓住第一次运行和第二次运行之间的所有元素,抱歉。

1 个答案:

答案 0 :(得分:1)

您遇到的问题是<form action="file.php" method="post"> Name: <input type = 'text' name = "InputName" > Email: <input type = 'text' name = "InputEmail" > <input type='submit' value='Submit'> </form> // Open the text file $f = fopen("textfile.txt", "w"); // Write text fwrite($f, $_POST["InputName"]); fwrite($f, $_POST["InputEmail"]); // Close the text file fclose($f); ?> 如果低&gt;则会被破坏0

mid1

如果你想让mid1和mid2成为差异的1/3和2/3,你需要这样写

mid2

或者你可以把它写成一个带

的衬里
int mid1 = (int) Math.floor((low+high)/3);
int mid2 = (int) Math.floor((low+high)/1.5);

此代码

int dif = high - low;
int mid1 = low + dif / 3;
int mid2 = high - dif /3;
//or
int mid2 = low + 2 * dif / 3;

还假设int mid1 = (2 * low + high) / 3; int mid2 = (low + 2 * high) / 3; 否则你想要的是

int[] a = new int[mid1+1];
int[] b = new int[mid1+1];
int[] c = new int[mid1+1];

最后这条线毫无意义

low == 0

如果int[] a = new int[mid1 - low]; // how many in the first part int[] b = new int[mid2 - mid1]; // how many in the second part int[] c = new int[high - mid2]; // how many in the third part 包含split(a, a[0], a[a.length - 1]); ,您就不会将a分割为[100, 200, 300]。你可能的意思是

100
您将注意到的

实际上是

300

意味着您不需要随时获取阵列的副本。