Java未排序列表数组

时间:2014-09-02 07:21:57

标签: java arrays list

我正在处理一个项目,以下方法无法正常工作。 它应该创建一个随机的数字列表,然后列表上的方法应该像这样工作: 查找(找到值) 删除(删除值)应调用find以获取实际号码的位置 全部删除(删除列表中的所有数字n) 找到所有(不同数组位置上的相同数字)

删除例如,当我要查找的号码未包含在列表中时,删除列表中的适合项目。应该从删除调用我不知道如何实现delete all / find all

package bp;


public class UnsortedList implements IUnsortedList {

    public final int MAX_SIZE = 10000;
    public final int MAX_VALUE = 10;
    private int sizeOfList = 0;
    private boolean duplicatesAllowed = true;
    private final int[] listItems = new int[MAX_SIZE];
    int searchKey;
    int f;
    int n;


    @Override
    public int getSizeOfList() {
        return sizeOfList;
    }

    @Override
    public boolean areDuplicatesAllowed() {
        return duplicatesAllowed;
    }

    @Override
    public void setDupliatesAllowed(boolean pDuplicatesAllowed) {
        duplicatesAllowed = pDuplicatesAllowed;

    }

    @Override
    public void clear() {
        sizeOfList = 0;

    }

    @Override
    public void insert(int pValueToInsert) {
        //PLace the value in the last index position and increase the size of list
        if (sizeOfList < MAX_SIZE) {
            listItems[sizeOfList++] = pValueToInsert;
        }
    }

    @Override
    public void delete(int pValueToDelete) {
        int positionToDelete = find(pValueToDelete);
        searchKey = pValueToDelete;
        for ( f = 0; f < sizeOfList; f++)
            if(listItems[f] == searchKey)
                break;
        for (int k = f; k < sizeOfList-1; k++)
            listItems[k] = listItems[k+1];
        sizeOfList--;

    }

    @Override
    public void deleteAll(int pValueToDelete) {

    }

    @Override
    public void initializeWithRandomData(int pSizeOfList) {

        if(duplicatesAllowed) {
            for (int n=0; n < pSizeOfList; ++n) {
                listItems[n] = (int) (Math.random() * MAX_VALUE +1);
                ++sizeOfList;
            }
        } else {
            for (int n= 0; n < pSizeOfList; ++n) {
                int newvalue = (int) (Math.random() * MAX_VALUE +1);
                while (find(newvalue) > -1) {
                    newvalue = (int) (Math.random() * MAX_VALUE +1);
                }

                listItems[n] = newvalue;
                ++sizeOfList;
            }
        }

    }

    @Override
    public int find(int pValueToFind) {
        searchKey = pValueToFind;
        for (n=0; n < sizeOfList; n++)
            if (listItems[n] == searchKey)
                break;
            if (n == sizeOfList)
                System.out.println("Can't find " + searchKey);
            else
                System.out.println("Found " + searchKey);

        return 0    ;
    }

    @Override
    public int[] findAll(int pValueToFind) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int n = 0; n < sizeOfList; n++) {
            sb.append(listItems[n]);
            sb.append(", ");
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }
}

1 个答案:

答案 0 :(得分:0)

您的find方法存在问题...

依赖于类实例变量的一部分,它在使用之前从未初始化,您从方法中返回0 ...

public int find(int pValueToFind) {
    //...
    return 0;
}

因此调用此方法的结果将始终为0,因此第一个元素被删除...

相反,首先,将n初始化为0,以便您始终从头开始搜索,然后在完成后返回n ...

public int find(int pValueToFind) {
    searchKey = pValueToFind;
    n = 0;
    //...
    if (n == sizeOfList) {
        n = -1;
    }

    return n;
}