Created
December 25, 2012 19:33
-
-
Save ffbit/4375001 to your computer and use it in GitHub Desktop.
Removing from Java List by int and Integer idexes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: