Reputation: 79
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?
Upvotes: 0
Views: 1494
Reputation: 30889
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);
Upvotes: 1
Reputation: 76
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)
Upvotes: 0
Reputation: 493
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".
Upvotes: 1
Reputation: 21
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.
Upvotes: 1
Reputation: 98
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/
Upvotes: 1