Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Revising ArrayList Java. Full ArrayList class that I'm revising: http://users.cs.fiu.edu/~weiss/dsj3/code/weiss/util/ArrayList.java My goal is that when I add items to the array, they are added in

Revising ArrayList Java. Full ArrayList class that I'm revising: http://users.cs.fiu.edu/~weiss/dsj3/code/weiss/util/ArrayList.java My goal is that when I add items to the array, they are added in ascending order (like sorted). So, I need to 'modify' the add() method in arraylist (and any other necessary methods). I am not extending arraylist class.

class NewArrayList extends AbstractCollection implements List { private static final int DEFAULT_CAPACITY = 10; private static final int NOT_FOUND = -1; private AnyType [ ] theItems; private int theSize; private int modCount = 0; public NewArrayList( ) { clear( ); } //takes a collection and adds them to the, theItems array (not ascending). public NewArrayList(Collection other) { clear(); for (AnyType obj : other) add(obj); } public boolean addAll(int index, Collection c) { return false; } //gets the element associated with a particular index. public AnyType get(int index) { if(index < 0 || index >= size()) { throw new ArrayIndexOutOfBoundsException(); } return theItems[index]; } //sets an element at a particular index (reassignment). //returns the old value just in case we want it for some reason. public AnyType set(int index, AnyType setVal) { if(index < 0 || index >= size()) { throw new ArrayIndexOutOfBoundsException(); } AnyType oldVal = theItems[index]; theItems[index] = setVal; return oldVal; } //overridden version, unnecessary? public void add(int idx, AnyType element) { } //adds an element to the end of the Array, does dynamic array expansion if necessary //MODIFY THIS so that it adds elements in sorted order. //ex: {3} and I add 2, it should automatically put the two before the 3. ({2,3}). public boolean add (AnyType element) { 
if(theItems.length == size()) { AnyType [] oldVals = theItems; theItems = (AnyType []) new Object[theItems.length * 2]; for(int i = 0; i < size(); i++) { theItems[i] = oldVals[i]; } } theItems[theSize++] = element; modCount++; return true;
 I want to modify the bottom add. 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions

Question

In Exercises, find the limit. X x so - x -

Answered: 1 week ago