0

I have some code.

     @Override
        public void handle(ActionEvent event) {

            System.out.println(counter.get(0));

            fileHolder.remove(counter.get(0));

            try {
                        FileWriter writer = new FileWriter("videoGameDatabase.txt");
                        for (int i=0;i<fileHolder.size();i++) {

                        writer.write(fileHolder.get(i));

                        if(i < fileHolder.size()-1) writer.write("\r\n");

                        }

                        writer.close();
                    } catch (IOException ex) {
                        Logger.getLogger(FinalProject.class.getName()).log(Level.SEVERE, null, ex);
                    }

        }
    });

Here, I am trying to delete an element in an array list. When I try to use this button to delete the entry, it does not work. Counter's first element's value is 1.

However, when I do:

           fileHolder.remove(1);

it works perfectly fine, yet both values are 1.

Why does the first one not work but the second one does?

3
  • 2
    Please update the question with all of the code necessary to reproduce the issue. See stackoverflow.com/help/mcve. Commented Apr 16, 2016 at 21:35
  • can you show us the type of fileholder and counter. I suppose arraylist but wich datatype Commented Apr 16, 2016 at 21:37
  • Possibly related: stackoverflow.com/questions/4534146/… Commented Apr 16, 2016 at 21:42

3 Answers 3

1

One word: Autoboxing. The Java Collections Framework automatically boxes primitive values with their corresponding object, e.g. int is auto-boxed as an Integer before it is stored in a Collection such as an ArrayList. This causes ambiguity when using a List<Integer> because there are two remove methods, namely remove(int) and remove(Object).

Solution: You should use an explicit cast to call the correct remove method when using an ArrayList<Integer>.

  • Use list.remove((Integer)x) when removing by value
  • Use list.remove((int)x) when removing by index

Note: Do not use list.remove(new Integer(x)). This unnecessarily creates a new Integer instance every time it is called. Instead, use the explicit cast (as shown above) or the Integer.valueOf method; these both take advantage of the autoboxing cache.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this: fileHolder.remove((Integer)counter.get(0)); and it still did not work :/
Could you please capture the return value, as in boolean retVal = fileHolder.remove((Integer)counter.get(0));. Was it true or false?
Actually, i changed the Integer to an int and it worked :)
0

Please review the Javadocs for ArrayList. ArrayList has two remove methods, one of which takes an int, and removes at an index. The other takes an Object.

If you have stored integers in the ArrayList, and then attempt to remove by an int, you will remove by the index, not the object.

You need to carefully review the code for what is being returned. If counter.get(0) returns an int, you will then remove the object at the specified index.

2 Comments

counter does return an int. How can I remove the element? :/
@elemein, is the counter returning the index that should be removed? If so, you should be able to do fileHolder.remove((int)counter.get(n));. If it is returning the value that is supposed to be removed, you should be able to do fileHolder.remove((Integer)counter.get(n));. Though others may disagree, I would probably do something more like int idx = counter.get(n); fileHolder.remove((int)idx); or Integer val = counter.get(n); fileHolder.remove((Integer)val); to make more clear the intent and the contents.
0

I can't see how you read/store values from/in an array. Maybe there's a problem - reading always first value....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.