0
public class SortedListOfImmutables {


 // Assume that I already have constructors that create new objects,
 // thus have its own items array.

private Listable[] items;

The method below removes an item from the list.If the list contains the same item that the parameter refers to, it will be removed from the list. If the item appears in the list more than once, just one instance will be removed. If the item does not appear on the list, then this method does nothing. @param itemToRemove refers to the item that is to be removed from the list

public void remove(Listable itemToRemove) {

    Listable[] newList = new Listable[items.length - 1];

    int count = 0;

    if(items.length == 0){
        newList[0] = itemToRemove;
    }else {
 /*Compares objects. If they are equal, I replace the index of items
  * to null. I use int count to make sure that it only makes one object
  * null.
  */
        for(int i = 0; i < items.length; i++){
            while(count == 0){
                if(items[i].equals(itemToRemove)){
                    items[i] = null;
                    count++;
                }
            }
        }
    }

    int changeVar = 0;

 /* Copy all the objects into my newList array. Wherever items is null,
  * skip to the next index of items and put it into newList.
  */
    for(int i = 0; i < newList.length; i++){
        newList[i] = items[i + changeVar];
        if(items[i + changeVar] == null){
            changeVar += 1;
            newList[i] = items[i + changeVar];
        }
    }

    items = newList;

}

When I run this I get a timeout error. What did I do wrong and how can I fix it. Note: I am not allowed to use ArrayList, HashSet, or LinkedList,

3
  • Why are you not allowed to use ArrayList, HashSet, or LinkedList? Commented May 6, 2015 at 0:29
  • what is the value of items.length ? Commented May 6, 2015 at 0:29
  • @ Tirupati Rao: limits.length could have any value. @ Iqbal: My computer science class wants me to hard code it before I learn ArrayList, HashSet, or LinkedList. Commented May 6, 2015 at 0:39

1 Answer 1

1

Change your second for loop to

int j=0;
for (int i = 0; i < items.length; i++) {
        if (items[i] ! = null) {
            newList[j] = items[i];
            j++;
        }
}

This should work for you.

Edit:

Remove the while (count==0) loop, this is creating the timeout.

Sign up to request clarification or add additional context in comments.

4 Comments

It was a good shot, but I am still getting a "timeout error" when I run my professors tests
Check my Edit in the above answer, it shall help you. The while loop is creating timeout. Just remove it. Hope this helps.
If I remove my While loop, then wont I have multiple nulls if I have objects that are duplicates? I am supposed to remove a specific object from an array of objects, but if there are multiple objects that are the same, I can only remove one of them.
you're right about the while loop giving a timeout though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.