The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Write a method called removeLast that removes and returns the last value from a list of integers. For example, if a variable called list stores [8, 17, 42, 3, 8], a call of list.removeLast(); should return 8 and change the list's state to [8, 17, 42, 3]. The next call would return 3 and remove 3 from the list, and so on. if the list is empty, throw a NoSuchElementException.
Â
package removeLast;import java.util.*;import java.util.Arrays;import java.util.Collection;import java.util.Iterator;import java.util.NoSuchElementException;//Class ArrayIntList can be used to store a list of integers.public class ArrayIntList implements Iterable<Integer> {private int[] elementData;// list of integersprivate int size = 0;// current number of elements in the listpublic static final int DEFAULT_CAPACITY = 10;// post: constructs an empty list of default capacitypublic ArrayIntList() {this(DEFAULT_CAPACITY);}// pre : capacity >= 0// post: constructs an empty list with the given capacityprivate ArrayIntList(int capacity) {if (capacity < 0) {throw new IllegalArgumentException("Capacity must be at least 0: " +capacity);}elementData = new int[capacity];}// for creating test cases more easilypublic ArrayIntList(int... elements) {this(Math.max(DEFAULT_CAPACITY, elements.length * 2));for (int n : elements) {add(n);}}// for creating test cases more easily (a dupe of the above constructor)public static ArrayIntList withValues(int... elements) {ArrayIntList list = new ArrayIntList(Math.max(DEFAULT_CAPACITY,elements.length * 2));for (int n : elements) {list.add(n);}return list;}// for creating test cases more easilypublic ArrayIntList(int element, boolean notCapacity) {this();add(element);}// for creating test cases more easilypublic ArrayIntList(Collection<Integer> elements) {this(Math.max(DEFAULT_CAPACITY, elements.size() * 2));for (int n : elements) {add(n);}}// copy constructor; for creating test cases more easilypublic ArrayIntList(ArrayIntList list) {this(Math.max(DEFAULT_CAPACITY, list.size() *Â