簡體   English   中英

如何從數組中刪除所有相同的事件

[英]how to delete all the same occurences from an array

到目前為止,我已經嘗試創建下面的方法,但是當我運行它時,新數組將零留給空白空間。 如果創建了所有查找方法來使用此方法,該如何用二進制搜索而不是線性搜索來實現

package bp;

import java.util.Arrays;

public class SortedList implements IUnsortedList {

    /**
     * The max size of the List.
     */
    public static final int MAX_SIZE = 10000;
    /**
     * The max value of each occurence.
     */
    public static final int MAX_VALUE = 10;
    /**
     * Flag for the amount of items on the list.
     */
    private int sizeOfList = 0;
    /**
     * Variable to define true or false for duplicates.
     */
    private boolean duplicatesAllowed = true;
    /**
     * Array saves the occurences in the list.
     */
    private final int[] listItems = new int[MAX_SIZE];
    /**
     * Variable for the value to find or delete.
     */
    private int searchKey;
    /**
     * Variable for counter in a loop.
     */
    private int f;


    @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) {
        //Loop finds the position of the Item
        for (f = 0; f < sizeOfList; f++)
            if (listItems[f] > pValueToInsert)
                break;
        //Loop moves the items after the position up
        for (int n = sizeOfList; n > f; n-- )
            listItems[n] = listItems[n - 1];
        //Insert the Value in the right position
        listItems[f] = pValueToInsert;
        //Increment List size
        sizeOfList++;
    }

    @Override
    public void delete(int pValueToDelete) {
        int destroyHAHAHA = find(pValueToDelete);
        //If it doesnt find it the item
        if (destroyHAHAHA==sizeOfList)
            System.out.println("I let you down boss, Can't find "
                    + pValueToDelete);
        //If it does, kill it with fire
        else {
            for (int n = destroyHAHAHA; n <sizeOfList; n++)
                listItems[n] = listItems[n + 1];
            sizeOfList--;
        }
    }

    @Override
    public void deleteAll(int pValueToDelete) {


        int j = 0;
        for(int i = 0;  i < listItems.length;  i++ )
        {
            if (listItems[i] != pValueToDelete)
                listItems[j++] = listItems[i];
        }
        int [] newArray = new int[j];
        System.arraycopy(listItems, 0, newArray, 0, j );


    }

    @Override
    public void initializeWithRandomData(int pSizeOfList) {
        // Loop creates an array with certain number of elements
        if (duplicatesAllowed) {
            for (int n = 0; n < pSizeOfList; ++n) {
                insert(listItems[n] = (int) (Math.random() * MAX_VALUE + 1));

            }
        } else {
            int newvalue=0;
            for (int n = 0; n < pSizeOfList; ++n) {
                listItems[n] = newvalue++;
                ++sizeOfList;
            }
        }

    }

    @Override
    public int find(int pValueToFind) {
        searchKey = pValueToFind;
        int lowNumber = 0;
        int highNumber = sizeOfList - 1;
        int result;
        while (true) {
            result = (lowNumber + highNumber) / 2;
            if (listItems[result] == searchKey)
                return result;
            else if (lowNumber > highNumber)
                return sizeOfList;
            else {
                if (listItems[result] < searchKey)
                    lowNumber = result + 1;
                    else
                        highNumber = result - 1;
                }
        }
    }

    @Override
    public int[] findAll(int pValueToFind) {
        //Array with the location of item
        int[] answerArray = new int[sizeOfList];
        int searchIndex;
        int answerIndex = 0;
        for (searchIndex = 0; searchIndex < sizeOfList; searchIndex++) {
            if (listItems[searchIndex] == pValueToFind) {
                answerArray[answerIndex++] = searchIndex;
            }
        }
        if (answerIndex > 0) {
            return Arrays.copyOfRange(answerArray, 0, answerIndex);
        } else {
            return new int[0];
        }

    }

    @Override
    public String toString() {
        return Arrays.toString(Arrays.copyOfRange(listItems, 0, sizeOfList));
    }

    public void bubbleshort(){
        int out;
        int in;
        int middle;

        for (out = 0; out < sizeOfList - 1; out++) {
            middle = out;
            for(in = out +1; in < sizeOfList; in++)
                if(listItems[in] < listItems[middle])
                    middle = in;
                    selectionSort(out, middle);
        }

    }

    public void selectionSort(int one, int two) {
        int temporal = listItems[one];
        listItems[one] = listItems[two];
        listItems[two] = temporal;
    }


}

您可以使用通用ArrayUtils.removeElement()ArrayUtils.removeAll()方法從數組中刪除所有元素。

集不包含重復項。 您可以使用Set。

Set<T> mySet = new HashSet<T>(Arrays.asList(someArray));

要么

Set<T> mySet = new HashSet<T>();
Collections.addAll(mySet, myArray);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM