4

I have a quiz program but a small section is not working. Basically, I have an ArrayList<Object> that holds 10 (or more, doesnt matter) Question Objects that I can refer to later.

Anyway, when I want to use ArrayList.remove(index), it doesn't remove that object! I honestly do not know what to do.

(Key Search is another class that takes in a string (key) and searches the quiz arraylist for it. It returns an arraylist of the questions that DO NOT contain the key word, thus letting me remove them from the overall question array.)

        ArrayList<Integer> indexAt = new ArrayList();
        indexAt = keyWord.indexAt(); //returning the questions that need to be removed
        System.out.println("(1) Number of Questions: " + Questions.size()); //debugging
        for(int i = 0; i < indexAt.size(); i++)
        {

            Questions.remove(indexAt.get(i));

        }

        //prints out the questions that were removed (for my own reasoning)
        for(int i = 0; i < indexAt.size(); i++)
        {

            System.out.println(indexAt.get(i));
        }

        System.out.println("(2) Number of Questions: " + Questions.size()); //debugging

        Sorting sort = new Sorting(Questions);

The output looks like this:


(1) Number of Questions: 10

1

2

3

4

6

(2) Number of Questions: 10


It should've removed indexes 1,2,3,4,6!!

Daniel Jo
  • 340
  • 2
  • 14
Jason
  • 101
  • 1
  • 1
  • 8
  • Then shouldn't my number of questions be 5, since I removed 1,2,3,4,6 from the Questions arraylist and therefor, when I use Questions.size(), it should return 5, not 10 – Jason Dec 26 '15 at 16:53
  • can you add code of `Questions` for further clarification? – thegauravmahawar Dec 26 '15 at 16:57
  • 1
    Make sure your `remove` is using ints, as objects will remove as keys and not indexes (might not find keys). – CosmicGiant Dec 26 '15 at 17:03
  • Hmm, when I hardcode in numbers instead of "indexAt.get(i)", it is working. Why would my indexAt (of type Integer) not work..) – Jason Dec 26 '15 at 17:08
  • 1
    I don't think that you're removing by index here. Since 'indexAt' returns a subtype of Object it might try to remove the passed object from the 'Questions' list. (in other words, I don't think it unboxes the integer to int) – Tom Dec 26 '15 at 17:10
  • Side note: `Questions` is not following Java's variable naming conventions. – CosmicGiant Dec 26 '15 at 17:18

1 Answers1

9

See ArrayList#remove(Object):

Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

and ArrayList#remove(int):

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

You use the first method, but you can change it easily:

Questions.remove(indexAt.get(i).intValue());

but you have also order your indexAt descending.

dur
  • 15,689
  • 25
  • 79
  • 125
  • This is perfect! Now it makes sense. Now, when I use it in a for loop, the remove obviously removes specific locations but in the next iteration, the locations will be messed up – Jason Dec 26 '15 at 17:26
  • @Jason *" Shifts any subsequent elements to the left (subtracts one from their indices)."* might cause problems with your current implementation. Just be advised. – CosmicGiant Dec 26 '15 at 17:29
  • @AlmightyR exactly.. – Jason Dec 26 '15 at 17:30