Eons384
Eons384

Reputation: 25

Array trouble deleting duplicates

This is for class homework. She is making us use array and array list in two parts. Basically she is showing us how using arraylist is alot easier than arrays.

I am having alot of trouble getting the array part to work.

  1. Create a class called CustomerLister1 with a main method that instantiates an array of String objects called customerName. The array should have room for six String objects. Use an initializer list to put the following names into the array:

    Chris  
    Lois  
    Meg  
    Peter  
    Stewie
    
  2. Write an enhanced for loop to display the array of names. What is displayed for the last array element? Why is it that value?

  3. Add the Strings "Meg" and "Brian" into index 3, and 4, respectively, so that the array contains the following elements:

    Chris  
    Lois  
    Meg  
    Meg  
    Brian  
    Peter  
    Stewie
    
  4. Write an enhanced for loop to display the array of names.

  5. Write a second, traditional for loop that checks each element for the String “Meg”, if found in the array, remove it, shift the remaining elements, and display the array of names. Are both instances of "Meg" removed correctly from the array?

This is my code

public class CustomerLister1
{

public static void main(String[] args)
{
    String[] customerName = new String[7];
    customerName[0] = "Chris";
    customerName[1] = "Lois";
    customerName[2] = "Meg";
    customerName[3] = "Peter";
    customerName[4] = "Stewie";

    for (int i = customerName.length-1;i > 3; i--)
    {
        customerName[i] = customerName[i - 2];
    }
    customerName[3] = "Meg";
    customerName[4] = "Brian";

    for (int m = 0; m <= customerName.length-1; m++)
    {
        if(customerName[m].equals("Meg"))
        {
            for(int j = m+1;j < customerName.length-1;j++)
            {
                customerName[j]= customerName[j-1];

            }

        }

    }

    for (String element : customerName)
    {

        System.out.println(element);
    }

}
}

I have tried messing with the element position. It won't print right because I have meg back to back and it replaces the first meg with the second meg. What can i do here?

Upvotes: 0

Views: 878

Answers (3)

user1618143
user1618143

Reputation: 1748

Change your if statement like this:

if(customerName[m].equals("Meg"))
{
    for(int j = m;j < customerName.length;j++)
    {
        if(j < customerName.length-2) {
            customerName[j]= customerName[j+1];
        } else {
            customerName[j]="";
        }
    }
m--;
}

By decrementing i by 1, you'll hit the same index again.

And also AmitD is correct; you need customerName[j+1], not [j-1].

Oh, and you'll need to handle the end of the array properly; don't want to try and set customerName[6] = customerName[7] when there is no customerName[7].

Edit: Ah, you'll need to start j at m, not m+1. That way you remove the "Meg" at customerName[m] as well as moving everything ahead of it backwards.

Edit: And the while part of the loop was skipping the last element in the array.

Upvotes: 1

mu_sa
mu_sa

Reputation: 2725

Not only you have to shift one element to the left but also put a nil value at the last index. So, i would propose something like

for(int j = m+1;j < customerName.length-1;j++)
{
    customerName[j]= customerName[j+1];
}
customerName[customerName.length-1] = "";

Upvotes: 0

John
John

Reputation: 16007

If you're only removing one of the Megs, it might be because you've skipped over the second one.

After you've shifted the elements after the removal, what element do you check next according to your code? Is it the one you need to check?

Upvotes: 0

Related Questions