0

Remove Three
An ArrayList has an Integer 3 and several other numbers in it. What is the best way to remove that Integer 3?

Scenario 1 You know the location of the Integer 3 in the list.

Scenario 2 You don’t know the location of the Integer 3 in the list.

Problem statement
Modify the given scaffolding code so that you handle Scenario 1. If the desired integer is properly removed, the sum of all elements in the list will be matched with what the test case expects.

import java.io.*;
import java.util.*;
public class JavaTutorial{ 
    public static void main(String[] args){ 
        List<Integer> numbers = new ArrayList<Integer>(); 
        numbers.add(1); 
        numbers.add(2); 
        numbers.add(3); 
        numbers.add(3); 
        numbers.add(4);
        numbers.add(3); 
        System.out.println("ArrayList contains : " + numbers); 
        numbers.remove(3);
        System.out.println("ArrayList contains : " + numbers); 
        int sum = 0;
        for (int i: numbers)
            sum += i;
        System.out.println (sum);
    }
}

This is my question and Sample Code to edit with.
Can anyone help me solve the problem?

  • If you are searching for the number three, you can use the ArrayList's indexOf method – Logan Feb 26 '16 at 05:36
  • Can you comment a sample code? – Steve Paul Willson Feb 26 '16 at 05:38
  • 2
    What thoughts do you have towards the problem? It sounds like you just dumped an assignment here without an attempt. – OneCricketeer Feb 26 '16 at 05:39
  • Just use your ArrayList instance to call that method. Do some research, there is a Javadoc for it on Oracle's site. And if you are removing inside of a loop while searching for similar elements, be sure to decrement your counter – Logan Feb 26 '16 at 05:42
  • 1
    better question: [why are you creating a magic list of numbers](http://meta.stackexchange.com/a/66378)? Whatever you're doing here, putting ints in an ArrayList makes no sense. If this is homework, stackoverflow has [additional criteria](/help/on-topic) for asking homework questions that you should probably edit your post for. – Mike 'Pomax' Kamermans Feb 26 '16 at 05:42
  • I've attempted some possibilities. I've removed the number 3 from the list, and returned the sum without value 3, but still the Test Case fails. – Steve Paul Willson Feb 26 '16 at 05:42
  • Hint: `numbers.remove(3)` returns `true` if 3 was removed from the list and `false` if it did not remove 3. That will get you to the point of " If the desired integer is properly removed" – OneCricketeer Feb 26 '16 at 05:44

5 Answers5

1

Check the javadocs of ArrayList, there is a remove method which takes the index as an argument. https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove(int)

For the second case Iterate through the list to find "3" and remove it. Here is a good tutorial on it: http://www.mkyong.com/java/how-do-loop-iterate-a-list-in-java/

Stephen84s
  • 88
  • 6
  • 1
    Well, you've guided without direct answer. – vels4j Feb 26 '16 at 05:41
  • As a new user, you should read [this](http://meta.stackexchange.com/a/8259) on why link-only answers, while potentially helpful, are not ideal. Can you please include a full answer in addition to your links? – IanS Feb 26 '16 at 05:46
  • @IanS Just went through the post, but I feel for this question I still feel the links are better cause this looks more like an assignment and the user seems new to Java, in my first link I am exposing the user to javadocs which any serious java programmer should look at first. If we give out the answer like Joydeep I don't see why the user would even bother visiting the link. – Stephen84s Feb 26 '16 at 06:00
1

To Give you a hint, you need to modify between two System.out.println.

System.out.println("ArrayList contains : " + numbers);  
numbers.remove(3);  
System.out.println("ArrayList contains : " + numbers); 

The code would be straight forward.

For scenario 1, you already did it in your question.

For scenario 2, there would be an Java API which gives you a location of the number you are looking for.

List in Java API: https://docs.oracle.com/javase/7/docs/api/java/util/List.html
you could get the index number by using "indexof".

Community
  • 1
  • 1
Sean83
  • 493
  • 4
  • 14
1

For the scenario 1, if you know the index of the element that you want to remove then you can use arrayListName.remove(index)

For the scenario 2, if you want to remove the element you can find the first occurrence of that element by using arrayList.indexOf(element). But this only removes the first occurrence. You may iterate it until it returns -1 if you want to remove all the occurrences of the element.

1

For scenario 1, use remove(int index). Since the third item has index 2 (it is zero-based), you may remove it like this:

// Scenario 1:
numbers.remove(2);

For scenario 2, use remove(Object o). Just cast it to Integer and it will search for the object that equals 3:

// Scenario 2:
numbers.remove((Integer)3);

Test it:

List<Integer> numbers = new ArrayList<Integer>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);

// Scenario 1:
numbers.remove(2);

// Scenario 2:
numbers.remove((Integer)3);

System.out.println(numbers);

Note: In both scenarios, there is no need to iterate the list.

Update: In scenario 2, if the list may contain more than one number 3, then to remove all of them (Java 8) I would do:

numbers.removeIf(((Integer)3)::equals);
Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
0
import java.io.*;
import java.util.*;
public class JavaTutorial{ 
public static void main(String[] args){ 
    List<Integer> numbers = new ArrayList<Integer>(); 
    numbers.add(1); 
    numbers.add(2); 
    numbers.add(3); 
    numbers.add(4);
    System.out.println("ArrayList contains : " + numbers); 

    //Case 1 : 
    for(int k=0; k<numbers.size(); k++){
      if(numbers.get(k)==3){
        numbers.remove(k);
        break;
      }
    }

    //Case 2 (avoid loops for optimization):

    numbers.remove(numbers.indexOf(3));   

    System.out.println("ArrayList contains : " + numbers); 
    int sum = 0;
    for (int i: numbers)
        sum += i;
    System.out.println (sum);
}

}

NB : Both the approaches are dynamic (i.e. You dont know the index of the specific number)

Joydeep
  • 76
  • 9
  • That for loop really isn't necessary. `numbers.remove(3)` returns true if 3 was removed. Could also use `indexOf(3)` – OneCricketeer Feb 26 '16 at 05:48
  • You should explain why this works. [SO is not a code-writing service](http://meta.stackoverflow.com/questions/266270/specific-warnings-to-newbies-about-homework-code-writing-please-debug-and-sscce). – IanS Feb 26 '16 at 06:06
  • @ cricket_007 numbers.remove(3) doesn't remove the content 3 but it removes the content that is at 3rd index. In this case, numbers.remove(3) removes 4 from the collection. I should have used numbers.remove(numbers.indexOf(3)) for better optimization. – Joydeep Feb 26 '16 at 08:49
  • You are partially correct, but there are two remove methods. One that takes an index, and another that takes an Object. Please see http://stackoverflow.com/a/4534158/2308683 – OneCricketeer Feb 27 '16 at 13:50