无法使用书面copy()方法复制数组

时间:2016-01-20 06:13:43

标签: java arrays methods

所以这段代码的前提是建立一个它,因为我们在整个学期学习不同的东西,如节点等。

目前,在此版本的代码中,我需要替换使用0的任何内容来表示数字集合的结尾。所以,让我们说如果数组A有[1,2,5,4,0],那么0就是我们在数组末尾的指标。

现在在这个版本中,我们必须使用一个计数器(这个代码中的a.k.a howmany)来表示数字集合的结束。

目前,使用此代码,我发现了界限错误,而且我还没有完全理解原因。我在copy()方法和omit()方法中得到它。

这是主要课程:

import java.util.*;

public class Intcoll2client {

    public static final int SENTINEL = 0;

    public static void main(String[] args) {

        int value;
        Scanner keyboard = new Scanner(System.in);
        Intcoll2 P = new Intcoll2(), N = new Intcoll2(), L = new Intcoll2(5);

    System.out.println("Enter an integer to be inserted or 0 to quit:");
    value = keyboard.nextInt();

    while (value != SENTINEL) {
        if (value > 0) {
            P.insert(value);
            L.insert(value);
        } else {
            N.insert(-value);
            L.omit(-value);
        }
//            L.arraySize();
        System.out.println("Enter next integer to be inserted or 0 to quit:");
        value = keyboard.nextInt();
    }

    System.out.println("\nThe values in collection P are:");
    P.print();
    System.out.println("\nThe values in collection N are:");
    N.print();
    System.out.println("\nThe values in collection L are:");
    L.print();
    if (P.equals(N)) {
        System.out.println("\nP and N are equal.");
    } else {
        System.out.println("\nP and N are NOT equal.");
    }
    Intcoll2 A = new Intcoll2();
    A.copy(L);
    System.out.println("\nThe values in the copy of L are:\n");
    A.print();
    }
}

以下是我所使用的所有方法的课程:

import java.util.*;

public class Intcoll2 {

private int[] c;
private int howmany;

/**
 * Creates a new array c with the size 500
 */
public Intcoll2() {
    c = new int[500];
    howmany = 0;
}

/**
 * Creates a new array with the size i
 *
 * @param i positive integer
 */
public Intcoll2(int i) {
    c = new int[i];
    howmany = 0;
}

/**
 * Pre-condition: Takes in an object of Intcoll2 Post-condition: If obj does
 * not equal c, create a new array and copy all elements from c to obj
 *
 * @param obj
 */
public void copy(Intcoll2 obj) {
    if (this != obj) {
        c = new int[obj.c.length];
        int j = 0;
        while (obj.c[j] != 0) {
            c[j] = obj.c[j];
            j++;
        }
        c[j] = 0;
    }
}

/**
 * Pre-condition: Take in positive integer i 
 * Post-condition: Return true if
 * i is greater than 0 and i is equal to element in array. Otherwise, false
 *
 * @param i positive integer
 * @return
 */
public boolean belongs(int i) {
    int j = 0;
    while ((c[j] != 0) && (c[j] != i)) {
        j++;
    }
    return ((i > 0) && (c[j] == i));
}

/**
 * Pre-condition: Take in a positive integer i 
 * Post-condition: If i is greater than 0 and is not in the current collection, insert 
 * into collection. Otherwise, do nothing. If current array is too small to
 * insert new integer, create new array double the size and copy all
 * elements into new array. Redirect new array to old reference variable.
 *
 * @param i positive integer to insert in object
 */
public void insert(int i) {
    if (i > 0) {
        int j = 0;
        while ((j < howmany) && (c[j] != 0)) {
            j++;
        }
        if (j == howmany) {
            if (j == c.length) {
                int[] newC = new int[c.length * 2];
                for (int index = 0; index < c.length; index++) {
                    newC[index] = c[index];
                }
                c = newC;
            }
            c[j] = i;
            howmany++;
        }
    }
}

public void arraySize(){
    int size = c.length;
    System.out.println("Howmany: " + howmany);
    System.out.println("Size of L: " + size);
}

/**
 * if i is greater than 0, then scan array. If i is found in the array, take
 * away and move any element after the index to fill in the 0 space
 *
 * @param i positive integer
 */

    public void omit(int i) {
        if (i > 0) {
            int j = 0;
            while ((c[j] != 0) && (c[j] != i)) {
                j++;
            }
            if (c[j] == i) {
            int k = j + 1;
            while (c[k] != 0) {
                k++;
            }
            c[j] = c[k - 1];
            c[k - 1] = 0;
        }
    }
}

/**
     * Returns number of integers in a collection until loop hits 0
     *
     * @return howmany
     */
    public int get_howmany() {
        return howmany;
    }

    /**
     * Prints out the entire collection until it reaches an element of 0
     */
    public void print() {
        int j = 0;
        System.out.println();
        while (j < howmany) {
            System.out.println(c[j]);
            j++;
        }
    }

    /**
     * Pre-condition: Takes in an existing object of Intcoll1 Post-condition:
     * Compares two objects, if both collections are equal, return true.
     * Otherwise, false.
     *    
     * @param obj object containing an array
     * @return result
     */
    public boolean equals(Intcoll2 obj) {
        int j = 0;
        boolean result = true;
        while ((c[j] != 0) && result) {
            result = obj.belongs(c[j]);
            j++;
        }
        j = 0;
        while ((obj.c[j] != 0) && result) {
            result = belongs(obj.c[j]);
            j++;
        }
        return result;
    }
}

非常感谢任何类型的输入或帮助!

0 个答案:

没有答案