• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing all instances of an integer from an array

 
Leonard Brandeis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Relatively new here, and working on an excercise from a book that involves creating an array of random integers. I'm trying to write a method that will remove all instances of a particular integer from the array, which I've called list[]. It seems to work fine in most circumstances, but sometimes I get weird results. Here's the code:


I wrote a similar method that only removed the first occurence of a specified number from the list, and that method seems to work fine.
Any suggestions would be greatly appreciated. I'm trying to learn this on my own, so I don't have any one to ask...though I'd consider hiring a tutor if I could find one.

Thanks again.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What "weird results" do you sometimes get?
 
Leonard Brandeis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I'm guessing the problem stems from the fact that I have another method that adds an element to the array. If the array is already full, it calls on another method to double the size of the array by creating a new one and then assigning the new one to the original one. As a result, I may only have five integers in an array that can hold more. That's the reason for my count variable - so when I want to print the results, it will only print the array elements that have intentionally have integers in it, and not the entire array. In most cases, when I test my removeAll() method it works fine, but sometimes, after adding and deleting a few integers, I'll tell it to, for example, removeAll(1), yet when I print the results, there will still be a one in there - usually the last integer printed, sometimes in the second integer printed, sometimes in more than one place. I don't know if that makes any sense or not. I feel like this should be a relatively simple method to write, but I'm somehow making it more complicated than it needs to be. My removeFirst() method, which seems to work fine, definitely appears more complex than it needs to be - I've got a for loop embedded in a while loop, embedded in an if statement which is embedded in another for loop. But like I said, it's doing what I want it to do.

 
Dan Walin
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like you're doing a lot more looping than necessary. If I had to do this, I'd create a second array to hold the new set of integers in which you put every element from the original array except the ones you want to exclude. Then set the original array equal to that new array. This requires only looping through the original array once. Something like this:

 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with code was how it was being looped. Each time you would find a match , you would reset the array such that next elements would all start moving one position left. however the counter would keep on incrementing.
hence lets say you have an array 1,2,2,4. and you want to remove 2.

counter =0: not matched, nothing happens

counter=1: match found, resulting array, 1,2,2,4.

counter=2: match found, resulting array 1,2,4,4.... here you see the problem the previous value that was moved one position to the left is not scanned. hence one extra 2 remains.


my solution is very similar to yours. when match found i reset the array just like you have but additionally, decrement the original counter by one so that no element is missed scanning.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's try to remember to not provide complete solutions: the goal is to guide questioners in a general direction by providing hints.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David is correct; it helps nobody to provide a complete solution. If I had found out earlier I might have deleted the solution.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch Leonard Brandeis
 
Leonard Brandeis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well thank you everyone for the help and suggestions. I finally got it to work. I ran into some more trouble when testing the situation in which there was only one element in the array, but after walking away for a bit and returning I was able to resolve it.

David and Campbell, I appreciate your point about tips/hints rather than answers. My goal is to actually learn and understand this stuff.

Thanks again for the help and the welcome to Java Ranch.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Youi're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic