0

I want the following method to remove the inputted value and return the value if successful, however the "for" loop never actually removes any values.

Integer remove(Integer value)
    {
        if (_set_array.size() == 0)
        {
            return null;
        }
        else if (value == null)
        {
            return null;
        }
        else
        {
            for (int i = _set_array.size() - 1; i >= 0; i--)
            {
                if (value == _set_array.get(i))
                {
                    _set_array.remove(i);
                    return value;
                }
            }
            return null;
        }
    }
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
Monolithic
  • 13
  • 3

3 Answers3

0

It works fine for me. Can you check this code?

import java.util.ArrayList;
import java.util.Arrays;

class Main {
    public static void main(String[] args) {
        ArrayList _set_array = new ArrayList(Arrays.asList(10000, 20000, 30000, 40000, 50000));
        System.out.println(remove(30000, _set_array));
        System.out.println(_set_array);
    }

    static Integer remove(Integer value, ArrayList _set_array) {
        if (_set_array.size() == 0) {
            return null;
        } else if (value == null) {
            return null;
        } else {
            for (int i = _set_array.size() - 1; i >= 0; i--) {
                if (value == (int) _set_array.get(i)) { // cast Integer to int primitive type
                    _set_array.remove(i);
                    return value;
                }
            }
            return null;
        }
    }
}
Sandeepa
  • 3,457
  • 5
  • 25
  • 41
  • because the Integers are low values and in the constant pool. larger values wont == – slipperyseal Nov 28 '18 at 04:55
  • i changed 1,2,3,4,5 to 1000,2000,3000,4000,5000 and remove returns null. suggest value.equals(_set_array.get(i)) – slipperyseal Nov 28 '18 at 05:01
  • @slipperyseal agreed. Since here he is comparing the object type instead of primitive type its not working. So he can cast the value from Interger to int primitive type. I think it would be better since he is comparing int values other than objects. Whats your thoughts? – Sandeepa Nov 28 '18 at 05:13
  • you could also change the method param to int and each of these options would cause autoboxing to unbox the int from the object. personally i think value.equals(_set_array.get(i)) is less obscure about how it works as it is using the regular equals method on the Integer type – slipperyseal Nov 28 '18 at 05:15
  • why not use `equals` – Scary Wombat Nov 28 '18 at 05:20
0

use this for comparison:

value.intValue() == _set_array.get(i).intValue()

this would rather compare the int primitives represented by individual Integers.

AppleCiderGuy
  • 1,249
  • 1
  • 9
  • 16
-1

You are comparing references. Convert the references to primitive value using intValue method:

value.intValue() == _set_array.get(i).intValue()

Explained here : https://stackoverflow.com/a/53511942/1465553

Alternatively, as suggested in comments, you can use method equals to compare the two references.

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
  • Won’t the code in question throw an error? One is not supposed to modify the collection while iterating it in a for loop, right? – mettleap Nov 28 '18 at 04:49
  • the loop goes from the top the bottom, so it doesnt cause any index misalignment when elements are removed – slipperyseal Nov 28 '18 at 04:50
  • I was definitely comparing references. After converting them to primitives everything ran flawlessly. I should brush up on masking for sure. Thank you! – Monolithic Nov 28 '18 at 05:23