0

So far I have tried to create the method below, but when I run it, the new array leaves zeros for the empty spaces. If a find all method is created to work with this how can it be implemented with a binary search instead of a linear search

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;
    }


}
ajb
  • 31,309
  • 3
  • 58
  • 84
  • The following code (par of the program) searches for a single number by binary search. – user2856759 Sep 09 '14 at 00:05
  • 1
    `newArray = listItems` appears to undo the `arraycopy`. `arraycopy` copies elements into `newArray`. But `newArray` (like all array variables in Java) is a reference to an array; the assignment makes `newArray` point to the `listItems` array, which means that the array that you previously set up is now unreferenceable and will be thrown away. – ajb Sep 09 '14 at 00:09
  • when removed the result is still the same, an array with zeros at the end – user2856759 Sep 09 '14 at 00:12
  • do you want a new array with less entries than the original? or do you want an array of the same size as the original? – Barranka Sep 09 '14 at 00:12
  • lets say I have 1 2 3 4 5 6 7 7 7 7, when deleting 7, the new array should only contain 6 entries instead of 10 – user2856759 Sep 09 '14 at 00:15
  • `listItems` has 10000 elements. Do all 10000 of them contain valid data? If not, and if there are zeroes at the end, at some point you will need to create a smaller array, or tell `deleteAll` that you don't want to look at the entire array (by giving it the "real" length). It would help if we could see an actual SCCSE that demonstrates the problem. We can't see how `listItems` is being set up, or `deleteAll` is being called. – ajb Sep 09 '14 at 00:15
  • How does `deleteAll` know there are only 10 valid entries in `listItems`? – ajb Sep 09 '14 at 00:16
  • the console only prints out the amount of spaces used, even thought a 10000 spaces in thememory have been used – user2856759 Sep 09 '14 at 00:23
  • in the code I have an delete and find methods that does exactly what I need but don't have an idea of how to implement this for multiple occurrences (find all delete all) – user2856759 Sep 09 '14 at 00:24
  • possible duplicate of [Remove all zeros from array](http://stackoverflow.com/questions/8777217/remove-all-zeros-from-array) – Santiago Benoit Sep 09 '14 at 01:03
  • So even though `listItems` is always 10000 elements, you have a `sizeOfList` that keeps track of the actual number of elements. Great. But `deleteAll` doesn't change it when it deletes elements. – ajb Sep 09 '14 at 01:11

2 Answers2

0

You can use Common langs ArrayUtils.removeElement() or ArrayUtils.removeAll() method to remove all the elements from the array.

Mrunal Gosar
  • 4,595
  • 13
  • 48
  • 71
0

Set contains no duplicates. You can use a Set.

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

or

Set<T> mySet = new HashSet<T>();
Collections.addAll(mySet, myArray);
indika
  • 821
  • 7
  • 13