-1

I have a Generic method smallestValueInArray(T[] array) and this method gets an Array of any Type. This method looks like this:

public class Helper {

    public static <T extends Comparable<T>> T smallestValueInArray(T[] array) {
        T smallestValue = array[0];
        T smallerTempValue = array[0];

        for (int i = 0; i < array.length - 2; i+=2) {

            if (array[i].compareTo(array[i+1]) < 0) {
                smallerTempValue = array[i];
            } else {
                smallerTempValue = array[i+1];
            }

            if (smallestValue.compareTo(smallerTempValue) > 0) {
                smallestValue = smallerTempValue;
            }
        }
        return smallestValue;
    }
}

In the Main method I want to do something like this:

for (int i = 0; i < stringArray.length; i++) {
        someOtherArray[i] = Helper.smallestValueInArray(stringArray);
        Helper.deleteElement(stringArray, stringArray[i]);
    }

So I want to loop through stringArray, find the smallest element in that Array and add that element to a new array someOtherArray. After that I want to use a method deleteElement() and this method gets two parameters, first one is an Array, and the seccond one is the element position in that Array which should be deleted.

How should my deleteElement() method look like?

Important: I dont want to convert my array in a List and than use list.remove()!

assembler
  • 3,098
  • 12
  • 43
  • 84
Nedzad Ganic
  • 25
  • 1
  • 9

2 Answers2

5

If you don't want to convert the array to a List, the only option I see is to create a new array that doesn't contain the removed element. Your deleteElement method will have to return that array.

public static <T> T[] deleteElement(T[] array, int i)
{
    // create new array of length array.length - 1
    // copy all the elements from the source array except of the i'th element
    // return the new array
}

and you'll call it with:

stringArray = Helper.deleteElement (stringArray, i);

Of course, it would be much simpler to convert the array to ArrayList, remove the i'th element, and convert back to array.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • That is not a bad idea! – Nedzad Ganic Nov 15 '17 at 13:14
  • The only problem is how do i continue to loop over an original Array in my loop: 'for (int i = 0; i < stringArray.length; i++) {´' ' someOtherArray[i] = Helper.smallestValueInArray(stringArray); ' Helper.deleteElement(stringArray, stringArray[i]); ' } – Nedzad Ganic Nov 15 '17 at 13:25
  • @NedzadGanic You can't. You can't change the size of an array, so the only way to remove an element is to create a new smaller array. Perhaps you can consider first identifying all the elements you wish to remove (for example, by setting them to `null`), and only do the actual removal (via creation of new array) after the loop. – Eran Nov 15 '17 at 13:29
  • Ok, it is than just one more Java spesific "problem" – Nedzad Ganic Nov 15 '17 at 13:32
  • @NedzadGanic I don't know what you mean by that, but the loop you posted in the question doesn't make much sense. You should be iterating over the indices of `someOtherArray`, not `stringArray` (assuming you want to remove the smallest element in each iteration, and not the `i`'th element). – Eran Nov 15 '17 at 13:37
  • @Eran 'someOtherArray' is empty, and will be filled with smalles values from stringArray, because of that I am iterating over stringArray and searching for the smallest value – Nedzad Ganic Nov 15 '17 at 13:49
  • @NedzadGanic `someOtherArray` must be initialized to some length (and I'm assuming it's the same length as `stringArray`). – Eran Nov 15 '17 at 13:50
  • @AxelH can you than write an answer so i can see how I can do it properly? – Nedzad Ganic Nov 15 '17 at 13:51
  • @Eran i know, it is initialised exactly as you say – Nedzad Ganic Nov 15 '17 at 13:52
  • @NedzadGanic The answer is the same as Eran, but you need to use it correctly, it is `stringArray = Helper.deleteElement (stringArray, i);` not `Helper.deleteElement(stringArray, i);`. If you don't do this, you will not recover the new (shorter) array. Note that you send the `shortValue` instead of an `index` as expect here – AxelH Nov 15 '17 at 13:53
  • @AxelH I will try that out – Nedzad Ganic Nov 15 '17 at 13:54
0

Then use System.arraycopy, and the useful Arrays class.

static <T> T[] deleteAt(T[] array, int i) {
    int afteri = array.length - (i + 1);
    if (afteri > 0) {
        System.arraycopy(array, i + 1, array, i, afteri);
    }
    return array.copyOf(array, array.length - 1);
}

Mind that for int, double and other primitive types an overloaded function would be needed.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138