• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nested loop List matching  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm a beginner and my first project (maybe too much ambitious ) is a simple warehouse management system (getting the inspiration at work...).
I have two tables, one for model Item that have items and positions in warehouse, one for ItemVV that have items (every year items could be deleted).
I want to build a function that match the two lists of items and return items that doesn't appear in the new year.
Item could have many items with the same code but different positions
ItemVV have just one item with that code.






It doesn't work because when I get to two Item that have same code but different position (and different ID of course) only one get removed.. why?
Example:

cycle 1: allItem: iD 1 code A000 pos 15 carItem doesn't have A000 so nothing happens-> OK
cycle 1: allItem: iD 2 code A001 pos 10 carItem have A001 so remove it from copy-> OK
cycle 1: allItem: iD 3 code A001 pos 05 carItem have A001 but doesn't remove it.... WHY???

Inside "copy" remains all items that have another item with same code....



 
Sheriff
Posts: 4294
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we need to know what Item#modello is and how it's set up.  You probably should post your Item class
 
Saloon Keeper
Posts: 3336
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I notice right away, your "copy" reference is not a copy at all. It is just another reference to the same allItems() list.
 
Carey Brown
Saloon Keeper
Posts: 3336
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seeing as how your ItemVV list only contains one entry per modello, you could make a temporary HashSet<String> of the modello's. Then create an empty List<Item> to put reference in to of items not appearing in the hash set. You can use !set.contains(modello).

This assumes "modello" is a String.
 
Luca Cordero
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I think we need to know what Item#modello is and how it's set up.  You probably should post your Item class


Ye, sorry I forgot to edit...

modello is a String and is "code" in my explaination...

 
Luca Cordero
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Seeing as how your ItemVV list only contains one entry per modello, you could make a temporary HashSet<String> of the modello's. Then create an empty List<Item> to put reference in to of items not appearing in the hash set. You can use !set.contains(modello).

This assumes "modello" is a String.


Thanks!
I'll try it!
 
Luca Cordero
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:One thing I notice right away, your "copy" reference is not a copy at all. It is just another reference to the same allItems() list.


I'll also try if the problem is caused by this... Thank you!
 
Carey Brown
Saloon Keeper
Posts: 3336
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luca Cordero wrote:
Carey Brown wrote:One thing I notice right away, your "copy" reference is not a copy at all. It is just another reference to the same allItems() list.


I'll also try if the problem is caused by this... Thank you!

It's usually a bad idea to remove items from a list you're iterating through. The more robust way is to create a new empty list and then as you iterate, add selected items to the new list.
 
Sheriff
Posts: 11496
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class names are probably misleading. The ItemVV name seems random, like PersonXX or CalendarYY.  Make your class names reflect what they represent.  I doubt Item is an appropriate name for something that has both name and position in the warehouse.  Seems like you're making one class perform double duty.  If an Item can be found at different locations, then you probably need two separate classes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!