• Post Reply Bookmark Topic Watch Topic
  • New Topic

Removing unwanted elements in an array  RSS feed

 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



My aim is to generate a lists containing 1 to 100. I will then count the number of integers divisible by 3. After doing so, I want to delete the integers that are NOT divisible by 3 in the lists, could anyone help me? I treid doing it, but I seem to keep on getting the same lists. Thanks!

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using JDK 8 maybe ;) ?
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha, I'm using JDK 1.7 why? is it different with each version?!?!
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was very nice streams API introduced in JDK 8. But don't worry. Your task can be done without it .
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im confused....

So which part of the code is not working and where is the error? and, if possible, why?

Is it the code, or the API?
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I confused you.
The task you want can be done without problems using JDK 7.
JDK 8 just introduced some API that added another way to do this. If you are not using JDK 8 just forget what I said.

What you can do is:
create an empty list, then iterate through your array.
Add whatever you want to the list that matches your criteria (in this case numbers divisible by 3).
Then, if you really need it, convert your List to array using List#toArray
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep, I was thinking of doing that as well!

But the instructions in my assignment strictly says that I have to delete the items in the array lists and print it out again. Would that be possible using the code that I generated? Thanks!
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Do wrote:yep, I was thinking of doing that as well!

But the instructions in my assignment strictly says that I have to delete the items in the array lists and print it out again. Would that be possible using the code that I generated? Thanks!

Now I am confused. Is your requirement to use an array or a list? Or maybe an ArrayList?
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops! sorry, just an array!
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure? Your code says:The task will be much easier using ArrayList instead of int array.

The problem with your code is line 26. It overwrites all changes you did to the array!
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, but I havent learned Arraylist yet, so I want to play around with int[] Array and just make sure that I understand it well before moving to Arraylists.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I really suggest you learn Lists now. Especially when the task requires it.
Arrays can hold constant amount of data. They are not suitable for removal. I mean, this can be done but this might got complicated.
The List interface is what you need.
Just try it and it will take 3-times less code to do your task. All logic for removing an elemnt is within any of List implementations (let's say an ArrayList).

Just try it and ask here if you have any question about it.

Check here for more details:
List interface tutorial
List
ArrayList
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks heaps!

I'll try it out.....

One more question with the arrays......it seems that it is returning me the values like 2, 5, 8, 11 because I think Java rounds down to 0, any ideas how to fix it?
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Do wrote:Thanks heaps!

I'll try it out.....

One more question with the arrays......it seems that it is returning me the values like 2, 5, 8, 11 because I think Java rounds down to 0, any ideas how to fix it?

Could you be more specific? I don't understand what is the problem. Post the current version of your code.
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




So this is the latter part of the output :
0: 2 anArray
1: 3 anArray
2: 5 anArray
3: 6 anArray
4: 8 anArray
5: 9 anArray
6: 11 anArray
7: 12 anArray
8: 14 anArray
9: 15 anArray
10: 17 anArray
11: 18 anArray
12: 20 anArray
13: 21 anArray
14: 23 anArray
15: 24 anArray
16: 26 anArray
17: 27 anArray
18: 29 anArray
19: 30 anArray
20: 32 anArray
21: 33 anArray
22: 35 anArray
23: 36 anArray
24: 38 anArray
25: 39 anArray
26: 41 anArray
27: 42 anArray
28: 44 anArray
29: 45 anArray
30: 47 anArray
31: 48 anArray
32: 50 anArray
33: 51 anArray
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I stuffed something up
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with rounding. You have some logic error in your deleting method.
This is another thing that should make you use the ArrayList ;)
If you use it you won't have to worry about deletion logic. It will be already there.
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so here I go,

I have done it through ArrayList.....and you are right!

but it still seems that I am having a logic error, anyone care to help me out here? Thanks!





Output:
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HELP please! Thank you!
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you are iterating over the list. And you want to remove elements from that list, right?
So why not using an Iterator to do the iterating?
And then, you will be able to use Iterator#remove() to easily remove an element.

If you want to find the error in your looping/removing logic just follow on paper the code you have, writing down values of variables, etc. It should be clead...

And please don't write long lines ← click
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks dude!

I tried, I couldn't see the logic error though, could anyone help me out with it?

I know that the iterator is possible, but I want to try and make a program without it (for the time being).

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then "run" you code on paper exactly as it is written (not the way you think it works). And if you do that you'll spot the bug.
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your error is the traditional error which occurs when deleting entries from a list. Namely, when you delete an entry from a list then all of the entries after that one are renumbered, and you don't take that into account. My guess is that this error first occurred in a program written about 1955.

Here's how it works, if that short description didn't help. Start with "1 2 3 4 5 6" and remove all the odd-numbered entries.

Step 1: remove entry 1, leaving "2 3 4 5 6".

Step 2: remove entry 3, leaving "2 3 5 6". (Notice that "entry 3" now doesn't mean what it originally did.)

Step 3: remove entry 5 -- there isn't one, so we still have "2 3 5 6".

You can see that this algorithm doesn't produce "2 4 6".
 
ika khakhviashvili
Greenhorn
Posts: 2
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about if in the first version with arrays you just set not divisible arrays to "null"?
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why not try it? I don't know what will happen, but it might go horribly wrong!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!