0

Please help me with my code. i'm using an arraylist named tempAns, it contain values [2, 9, 2]. i've tried adding the last two numbers then replace them with the right answer so the result will be [2, 11]. but somehow, the output was always [9, 11]. it seems like it's deleting duplicate values.

   else if(scan.equals("+"))
            {
                double num2 = Double.parseDouble(tempAns.get(tempAns.size()-1));
                double num1 = Double.parseDouble(tempAns.get(tempAns.size()-2));
                double ans = num1 + num2;
                String stringAns = Double.toString(ans);
                System.out.println("before deleting: " +tempAns + "\n");

                tempAns.remove(tempAns.get(tempAns.size()-1));
                tempAns.remove(tempAns.get(tempAns.size()-1));

                System.out.println("before adding: " +tempAns);
                tempAns.add(stringAns);
                System.out.println(num1 + " + " +num2+ " = " +ans);
                System.out.println("after deleting: " +tempAns + "\n");
            }

6 Answers 6

4

The remove() method uses the equals() method to find which object should be removed. Then

 tempAns.remove(tempAns.get(tempAns.size()-1));

means

 tempAns.remove(2);

And the first element matches the equals, having the list with [9, 2]. Then, calling tempAns.remove(tempAns.get(tempAns.size()-1)); again, will remove the other 2, and the list will be [9].

You can use the remove(index) method or use LinkedList instead ArrayList, and call the removeLast() method twice.

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

2 Comments

oh, i see. thanks. but how do i fix it? i thought remove(int index_num)
@TokuchiToua see my answer, you used an inherited method, instead of the one from ArrayList.
1

One thing to consider is that you can consolidate the place where you retrieve num1 and num2 and where you remove them like such:

        System.out.println("before deleting: " +tempAns + "\n");
        double num2 = Double.parseDouble(tempAns.remove(tempAns.size()-1));
        double num1 = Double.parseDouble(tempAns.remove(tempAns.size()-1));
        double ans = num1 + num2;
        String stringAns = Double.toString(ans);

        System.out.println("before adding: " +tempAns);
        tempAns.add(stringAns);
        System.out.println(num1 + " + " +num2+ " = " +ans);
        System.out.println("after deleting: " +tempAns + "\n");

The previous code fixes the issue you were having and may be easier to follow as you are not trying to keep track of the offset where the items are in the list. This is a bit closer to popping the list off a stack and then pushing it back when you calculate the value.

1 Comment

thank you very much. i'm actually doing stack with the use of an arraylist.
1
tempAns.remove(tempAns.get(tempAns.size()-1));
//above remove is evaluated to 
tempAns.remove(2);  // Removes the 2 at index 0   

The problem is, you are first fetching the value at index 2.. which is 2. Now you are removing the value 2, which actually removes the 2 at index 0..

You should rather remove by index: -

tempAns.remove(tempAns.size()-1);

Comments

1

Use tempAns.remove(tempAns.size()-1) instead of using tempAns.remove(tempAns.get(tempAns.size()-1));

Comments

1
 tempAns.remove(tempAns.get(tempAns.size()-1)); You shold do below
 tempAns.remove(tempAns.size()-1); //remove last
 tempAns.remove(tempAns.size()-1); //remove second last

Reason is tempAns.get(tempAns.size()-1) returns 2 which is also at the start so remove will remove 2 at the start.

public boolean remove(Object o)

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).

Comments

1

The issue is that you are using the inherited method of remove, from AbstractCollection, when using: tempAns.remove(tempAns.get(tempAns.size()-1));. From your description you need the remove from ArrayList, that is using an index, so you should change the call to: tempAns.remove(tempAns.size()-1);

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.