0

I am trying to create a method that searches through the 'data' array to find the string 'elt'. If it exists, it shifts all elements after 'elt' one position to the left (to cover the cell where 'elt' existed).

I am able to find all instances of "elt" and set them to null, but I am having problems shifting all elements past "elt" down one space in the array. The code below is what I have so far.

public class Bag<T> implements Iterable<T> {

private final int MAXLEN = 3;
private int size;
private T[] data; // array

public T remove(T elt) {

        for (int i=0; i<data.length; i++) {
            if ("elt".equals(data[i]) ) {
                data[i] = null;

                for (i++; i < data.length; i++) {
                    data[i] = data[i-1];
                }
            }
        }
public static void main(String[] args) {
        Bag<String> sbag = new Bag<String>();

        sbag.add("Noriko");
        sbag.add("Buddy");
        sbag.add("Mary");
        sbag.add("Peter");
        sbag.add("elt");
        sbag.add("hello");

    Iterator<String> it = sbag.iterator();
        while (it.hasNext()) {
            String val = it.next();
            System.out.println(val);
        }

    sbag.remove("elt");

    Iterator<String> it2 = sbag.iterator();
        while (it2.hasNext()) {
            String val = it2.next();
            System.out.println(val);
        }
}

When I run that code, I get:

Noriko Buddy Mary Peter elt hello Noriko Buddy Mary Peter null

However, I am expecting

Noriko Buddy Mary Peter elt hello Noriko Buddy Mary Peter hello

Can anybody tell me how I can fix the code so that the rest of the items in the array are shifted down? I think the problem is in my remove method.

2
  • Why does your expected output contains etl and hello instead of null? Commented Oct 19, 2015 at 23:12
  • It should print Noriko Buddy Mary Peter let hello with the first iterator. Then, with the second iterator, I want it to print Noriko Buddy Mary Peter hello, because it removes elt from the array and shifts hello down in its place. However, it is not doing that. Commented Oct 19, 2015 at 23:16

1 Answer 1

1

If i understand what you're trying to achieve correctly, you're shifting the wrong way you want:

public T remove(T elt) {

    for (int i=0; i<data.length; ++i) {
        if (elt.equals(data[i]) ) {
            data[i] = null;
            for (++i; i < data.length; ++i) {
                data[i-1] = data[i];
            }
            break;
        }
    }

`

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

2 Comments

It worked, thank you! Would you mind explaining why this way worked but mine did not?
The issue with your code was the line data[i] = data[i-1]; it was replacing the null with the next position value ,because you have already incremented the index to next position . So it will something like data[5]=data[4](null)

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.