Question: java code ,i am lost insert method public class OrderedStringArrayList { private String[] array ; int index = 0; public OrderedStringArrayList() { array = new
java code ,i am lost insert method
public class OrderedStringArrayList { private String[] array; int index = 0; public OrderedStringArrayList() { array = new String[10]; } /** * _Part 1: Implement this method._ * * Inserts a new item in the OrderedArrayList. This method should ensure * that the list can hold the new item, and grow the backing array if * necessary. If the backing array must grow to accommodate the new item, it * should grow by a factor of 2. The new item should be placed in sorted * order using insertion sort. Note that the new item should be placed * *after* any other equivalent items that are already in the list. * * @return the index at which the item was placed. */ public int insert(String item) { int j = index; // If should grow if (size() == 0) { while (j > 0 && array[j - 1].compareTo(array[j]) > 0) { array[j-1]= array[j]; array[j] = item; j = j - 1; } index++; return index; } else if (size()== array.length) { String[] temp = new String[array.length * 2]; for (j = 0; j < size(); j++) { temp[j]=array[j]; while (j > 0 && array[j - 1].compareTo(temp[j])>0) { array[j - 1] = temp[j]; temp[j] = array[j]; array[j]=item; j = j - 1; } } for (j = 0; size() < 9; j++) { array[j]=item; System.out.println(array[j]); }} return index; } // Grow // Do insert /** * _Part 2: Implement this method._ * * @return the number of items in the list. */ public int size() { return array.length; } /** * _Part 3: Implement this method._ * * Gets an item from the ordered array list. You can assume that this method * will only be called with valid values of index. Specifically, values that * are in the range 0 - (size-1). To impress your friends and build your * street cred, consider adding checks that the index supplied is in fact in * bounds. If it is not, you can raise an IndexOutOfBoundsException. * * @param index the index to get an item from * @return an item at the specified index */ public String get(int index) { // TODO: implement this return null; } /** * _Part 3: Implement this method._ * * Counts the items in the ordered array list that are equal to the item at * the specified index. Be sure to take advantage of the fact that the list * is sorted here. You should not have to run through the entire list to * make this count. * * @param index an index in the range 0..(size-1) * @return the number of items in the list equal to the item returned by * get(index) */ public int countEquivalent(int index) { // TODO: implement this return 0; } /** * _Part 4: Implement this method._ * * Finds the location of the first object that is equal to the specified * object. Linear search is sufficient here, but for 5 points of extra credit * implement binary search. * * @param obj an object to search for in the list * @return the first index of an object equal to the one specified, or -1 if * no such object is found. */ public int find(String obj) { // TODO: implement this return 0; } /** * _Part 5: Implement this method._ * * Removes all the objects equal to the specified object. * * @param obj an object equal to the one(s) you'd like to remove * @return the number of objects removed */ public int remove(String obj) { // TODO: implement this return 0; } /** * This method is included for testing purposes. * Typically, you would not want to expose a private instance variable * as we are doing here. However, it does have value when the code is * going through a testing phase. Do not modify or remove this method. * Some Autolab tests may rely upon it. */ public String[] dbg_getBackingStore() { return array; } /** * _Part 6 (optional): Implement this_ * * Exercise your list to demonstrate it works as expected. */ public static void main(String[] args) { OrderedStringArrayList name= new OrderedStringArrayList(); name.insert("Bob"); name.insert("Alex"); name.insert("Ann"); // name.insert("John"); // name.insert("Jack"); //name.insert("Jordan"); ////name.insert("Stephen"); // name.insert("Maggy"); //name.insert("Donald duck"); //name.insert("Casper"); for (int j=0;j<9;j++){ System.out.println(name.array[j]); } // TODO: implement this return; } }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
