Skip to content

Instantly share code, notes, and snippets.

@mc256
Last active March 27, 2016 03:22
Show Gist options
  • Save mc256/5e6cf18b38eeb45b76ab to your computer and use it in GitHub Desktop.
Save mc256/5e6cf18b38eeb45b76ab to your computer and use it in GitHub Desktop.
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.Random;
// Michael Chen
// https://masterchan.me
// all rights reserved
public class MasteTest {
/**
* Write a method called removeInRange that accept four
* parameters: a List of integers, an element value, a starting
* index, and an ending index. The method's behavior is to
* remove all occurrences of the given element that appear in
* the list between the starting index (inclusive) and the ending
* index (exclusive). Other values and occurrences of the given
* value that appear outside the given index range are not affected.
*/
public static List<Integer> removeInRange(List<Integer> li, int x, int start, int end)
{
List<Integer> result = new ArrayList<Integer>();
for (int i = 0; i < li.size(); i++) {
if ( (i < start ) || (i >= end) || (li.get(i) != x) ) {
result.add(li.get(i));
}
}
return result;
}
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<Integer>();
list1.add(0);
list1.add(0);
list1.add(2);
list1.add(0);
list1.add(4);
list1.add(0);
list1.add(6);
list1.add(0);
list1.add(8);
list1.add(0);
list1.add(10);
list1.add(0);
list1.add(12);
list1.add(0);
list1.add(14);
list1.add(0);
list1.add(16);
System.out.println(list1);
System.out.println(removeInRange(list1, 0, 5, 13));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment