Created
May 30, 2017 18:02
-
-
Save btforsythe/ab685f658ec37e78593ee5cfb340d1cb to your computer and use it in GitHub Desktop.
Numbers problem
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
| package org.wecancodeit.objects.arraylists; | |
| import java.util.ArrayList; | |
| public class Numbers { | |
| public static void main(String[] args) { | |
| ArrayList<Integer> numbers = new ArrayList<Integer>(); | |
| numbers.add(42); | |
| numbers.add(23); | |
| numbers.add(86); | |
| // to remove 23, we can assign it to an Integer (object) so that it uses | |
| // the remove(Object) method instead of the remove(int) method | |
| // the below is the same as Integer asInteger = new Integer(23); | |
| // Java 'boxes' this primitive for us (autoboxing). | |
| Integer asInteger = 23; | |
| numbers.remove(asInteger); | |
| System.out.println(numbers); | |
| // we can also find the index of a value, then remove that index | |
| int index = numbers.indexOf(42); | |
| numbers.remove(index); | |
| System.out.println(numbers); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment