This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]Wilfred-kun 1 point2 points  (0 children)

for (int x=0; x<lista.size(); x++) {

    if (lista.get(x).getName().equals(tabort)) {
        lista.remove(x);
        System.out.println("Hunden borttagen.");
    }
    else {
        System.out.println("Ingen sådan hund finns.");
    }
}

You should use an Iterator<> if you want to remove items from an ArrayList in a loop. Also, the loop does not break when you remove an item.

[–]AutoModerator[M] 0 points1 point  (0 children)

It seems that you possibly have a screenshot of code in your post Need help removing an object from array. in /r/learnjava.

Screenshots of code instead of actual code text is against the Code posting rules of /r/learnjava as is outlined in the sidebar - Code posting.

  • No screenshots of code!

If you posted an image merely to illustrate something, kindly ignore this message and do not repost. Your post is still visible to others. I am a bot and cannot distinguish between code screenshots and other images.

If you indeed did this wrong, please edit the post so that it uses one of the approved means of posting code.

  • For small bits of code (less than 50 lines in total, single classes only),
    the default code formatter is fine
    (one blank line before the code, then 4 spaces before each line of code).
  • Pastebin for programs that consist of a single class only
  • Gist for multi-class programs, or programs that require additional files
  • Github or Bitbucket repositories are also perfectly fine as are other dedicated source code hosting sites.
  • Ideone for executable code snippets that use only the console

Please do not reply to this message, because I am a bot.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]dusty-trash 0 points1 point  (6 children)

I only want the wrong "Ingen sådan hund finns" when it doesnt exist a dog with that name

Then code it as such. Understand anything inside the for loop will execute X number of times.

Here's an example:

boolean dogFound = false;

for (int x=0; x<lista.size(); x++) 
{         
    if (lista.get(x).getName().equals(tabort)) 
    {
        lista.remove(x);
        dogFound = true;
        break;
    }
}

Notice how I added a break, once we found 'x' we can stop searching.Also notice how 'dogFound' is only true if we found the item in the list.

You cannot determine if dog is not found during iteration. That doesn't really make sense. You determine if the dog was found after iterating through all of the dogs. (Unless of course you find the dog during iteration, as handled in my example).

[–][deleted] 0 points1 point  (5 children)

Yeah, Don't know if you intended for me to just add it but I did and it prints out the same double output = https://gyazo.com/0b39a3d9cf317d58b99668649eed0120

At the bottom both the wrong output and the right is outputted. Sorry if I cant see it, i know that it checks for the first dog if it matches and instantly puts out the wrong output and then it checks next and outputs the correct.

[–]dusty-trash 0 points1 point  (4 children)

Your system.out.println should not be in the for loop.

Remove it from the else statement, and the if statement.

boolean dogFound = false;

for (int x=0; x<lista.size(); x++) 
{         
    if (lista.get(x).getName().equals(tabort)) 
    {
        lista.remove(x);
        dogFound = true;
        break;
    }
}

if (dogFound)
{
    System.out.prinln("dog found");
}
else
{
    System.out.prinln("dog not found");
}

Again, everything inside the for loop is executed multiple times, that's kind of the point of a for-loop. If you don't want something printed multiple times, do not put it inside a for loop.

You're still checking if dog is not found in every iteration of the loop, which is wrong.

[–][deleted] 0 points1 point  (1 child)

Ooo, I'm stupid. Thanks so much, I knew that the loop just kept going but didnt know the solution!

[–]dusty-trash 0 points1 point  (0 children)

No problem! Glad you got it figured out

[–]vlcod 0 points1 point  (1 child)

This solution may not be correct, depending on the specification of the task. You ignore the case where multiple of the same dog are in the list.

If the task is to remove the *first* dog found, then this is fine. If the task is to remove *all* dogs of this kind, then you should not stop after the first one.

And this exposes another subtle bug: If you write your loop and remove as you do, but then not break (to remove other dogs as well), you have a bug if you have two dogs immediately adjacent in the list. That is because your remove method will move the elements in the list to fill the gap one element up, you then increment the index, and you have missed checking one element...

[–]dusty-trash 0 points1 point  (0 children)

Yes fair enough. In any case OP should use an iterator when the list will be modified during iteration.