0

I'm working in Java right now, and I'm attempting to make an array copy itself into a temporary array, and then overwriting again with a new list, however the code isn't functioning.

MobilePhoneInventory.java

public void delete(MobilePhone p) {
    MobilePhone[] temp = new MobilePhone[list.length-1];
    int adj = 0;
    for (int i = 0; i < list.length; i++) {
        if (p != list[i]) {
            temp[i-adj] = list[i];
        }
        else adj = 1;
    }
    list = temp;
    numMobilePhone--;
}

MobilePhoneStart.java

    if (e.getSource() == myDelete) {
        if (inven.size() > 1) {
            inven.delete(inven.get(see));
            see--;
            if (see < 0) {
                see = 0;
            }
            showMP();
        } else {
            System.exit(0);
        }
    }

For some reason it's not creating the new list. Any ideas? Thanks in advance.

10
  • 7
    I would use ArrayList or have a look at how it does it. Commented Nov 29, 2013 at 16:27
  • I'm not allowed to use arraylist in this. Commented Nov 29, 2013 at 16:28
  • Why would you not use array.clone to get the new array? Commented Nov 29, 2013 at 16:29
  • I need to remove an object from the array. Commented Nov 29, 2013 at 16:30
  • 2
    @user3033222 and you are not allowed to read the code from ArrayList or learn anything from it? Commented Nov 29, 2013 at 16:31

1 Answer 1

3

When copying Java arrays, my preferred way is with System.arrayCopy. In your case, you have a test inside the loop which actually probably makes your approach more suitable.

Without seeing the rest of your class and how its mutable state works, it's hard to be sure about the solution. Overall, it looks OK. Possibly, there is a mistake with a reference somewhere so you don't get the thing you thought you'd got.

But one thing worth testing is that your equality test is the correct one:

if (p != list[i]) {

because you may find a ! ... .equals test works more reliably than !=.

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

5 Comments

I have the same result as the way I had it before. It doesn't remove the object from the array.
I like this answer. You should prefer equals() over ==. The former is testing equality as you define it in the class definition, while the latter is testing whether you are looking at the exact same instance, which may not be what you mean here. The default equals() implementation just calls equals() on each field in an object - you may want to do more (or less). If you do write your own implementation of equals() on MobilePhone, make sure you read this link: (stackoverflow.com/questions/2265503/…)
This ins't the answer. It's a good practice but it didn't solve the problem.
How do you identify a MobilePhone? Is it with some kind of id field and nothing more? If so, you should write your own equals() method for MobilePhone that just compares id. Otherwise the default equals() methods may be comparing fields you do not care about in two MobilePhone instances with the same id, and returning false for equality.
"For some reason it's not creating the new list" - it clearly is, but maybe it gets lost due to overwriting the wrong field or being overwritten by a subsequent alteration. Could you check how the rest of the class works, because the bug may not be in the code you listed.

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.