Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

ArrayList thinks it contains new entry

 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings, I am wondering if anyone can give me a nudge with some code I have been debugging since yesterday.

I am sorting pixels stored as int[2] into groups. Each group is an ArrayList<int[]> called group. The groups are collected into a Hashtable called groups.

For each new pixel x,y I want the routine to run through all the groups in the Hashtable and see if x,y is already in one of the groups . If the pixel is already in one of the groups, it breaks this loop and if it is not there, it starts a new group.

My ArrayList appears to think every pixel it comes across is already in the group. I cannot figure it out! Perhaps the problem is visible from this snippet:





Considering pixel: 1 1 on groups size 0
adding to new group: 1 1
New group groupkey: 0
New group size is: 1
...
put in group number 0
Considering pixel: 1 2 on groups size 1
m is now: 0
test group size: 1
item number 0 is 1 2
...
Considering pixel: 2 1 on groups size 1
m is now: 0
test group size: 1
item number 0 is 2 1
...


And so, the routine always thinks the new number and nothing else is in the group. I am totally stumped. Any help would be most welcome. Thank you, Eric.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Eric Barnhill,

Can you please post the complete logic of it? At least the complete part which deals with the thing you've explained.
e.g. I don't see where the boolean variable alreadyListed is being used, or the part which puts the group in Hashtable.
By the way, any specific reason to use Hashtable?

All you'll need is:
1) Iterate over Hashtable
2) For each value,
iterate over the list, and check if your pair is in that list.

On the other hand, you can write a wrapper class for your array of length 2, and write appropriate equals method so that you don't have to iterate over ArrayList. All you'll need to do is - create an ArrayList of your new class, wrap the pair of pixel co-ordinates in new class' object, and invoke 'contains' method of ArrayList.

I hope this helps.
 
Eric Barnhill
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Anayonkar, thank you very much for your reply and I am happy to post the entire method.

I implemented a Hashtable with integer keys since I may want to later re-key the groups by a factor in a later method. But, I could use an ArrayList of ArrayLists if you think the snag may lie there.

Obviously all the println's are a desperate attempt to debug it!



Output in a 4x4 test matrix yields:


Considering pixel: 1 1 on groups size 0
adding to new group: 1 1
New group groupkey: 0
New group size is: 1
0 1 outside threshold
2 1 outside threshold
1 0 outside threshold
1 2 outside threshold
put in group number 0
Considering pixel: 1 2 on groups size 1
m is now: 0
test group size: 1
item number 0 is 1 2
test group size: 1
we are on item: 0
the contents of this are: 1 2
1 2 is the same as 1 2
already grouped :1 2
groupKey is 0
0 2 outside threshold
2 2 outside threshold
1 1 outside threshold
1 3 outside threshold
put in group number 0
Considering pixel: 2 1 on groups size 1
m is now: 0
test group size: 1
item number 0 is 2 1
test group size: 1
we are on item: 0
the contents of this are: 2 1
2 1 is the same as 2 1
already grouped :2 1
groupKey is 0
1 1 outside threshold
3 1 added to group
New group size is: 2
2 0 outside threshold
2 2 outside threshold
put in group number 0
Considering pixel: 2 2 on groups size 1
m is now: 0
test group size: 2
item number 0 is 2 2
test group size: 2
item number 1 is 2 2
test group size: 2
we are on item: 0
the contents of this are: 2 2
2 2 is the same as 2 2
already grouped :2 2
groupKey is 0
test group size: 2
we are on item: 1
the contents of this are: 2 2
2 2 is the same as 2 2
already grouped :2 2
groupKey is 0
1 2 outside threshold
3 2 added to group
New group size is: 3
2 1 outside threshold
2 3 added to group
New group size is: 4
put in group number 0


Many thanks!

Eric
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Barnhill wrote:I implemented a Hashtable with integer keys since I may want to later re-key the groups by a factor in a later method...


Seems to me you're going all around the houses, when all you need is a class that encapsulates your x,y point (Coordinate?).

There is, in fact, already one available: java.awt.Point, but it's so horrendous that I wouldn't even advise using it.
However, if you want to save time...

I suspect that much of the other stuff you want might be accomplished with a java.util.Comparator (or two).

Winston
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic