Skip to content

Instantly share code, notes, and snippets.

@btforsythe
Created May 30, 2017 18:02
Show Gist options
  • Select an option

  • Save btforsythe/ab685f658ec37e78593ee5cfb340d1cb to your computer and use it in GitHub Desktop.

Select an option

Save btforsythe/ab685f658ec37e78593ee5cfb340d1cb to your computer and use it in GitHub Desktop.
Numbers problem
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