Skip to content

Instantly share code, notes, and snippets.

@ffbit
Created December 25, 2012 19:33
Show Gist options
  • Select an option

  • Save ffbit/4375001 to your computer and use it in GitHub Desktop.

Select an option

Save ffbit/4375001 to your computer and use it in GitHub Desktop.
Removing from Java List by int and Integer idexes
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String... args) {
// Two identical list of fruit
final List<String> fruit = getFruit();
final List<String> fruit2 = getFruit();
// Banana's index
final int i = 1;
final Integer I = 1;
// Removing banana from the fruit list by its index
fruit.remove(i);
// Removing banana from the fruit list by its index
// Stop! Wait a minute, we're not removing by index here!
// Instead, we're removing Integer 1 as an element from the fruit2 list
fruit2.remove(I);
// Printing results
System.out.println("fruit = " + fruit);
System.out.println("fruit2 = " + fruit2);
}
private static List<String> getFruit() {
List<String> fruit = new ArrayList<String>();
fruit.add("apple");
fruit.add("banana");
fruit.add("pear");
return fruit;
}
}
@ffbit

ffbit commented Dec 28, 2012

Copy link
Copy Markdown
Author

Output:

fruit  = [apple, pear]
fruit2 = [apple, banana, pear]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment