This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes ArrayList thinks it contains new entry Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList thinks it contains new entry" Watch "ArrayList thinks it contains new entry" New topic
Author

ArrayList thinks it contains new entry

Eric Barnhill
Rancher

Joined: Feb 25, 2000
Posts: 233

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.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Eric Barnhill
Rancher

Joined: Feb 25, 2000
Posts: 233

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
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList thinks it contains new entry
 
Similar Threads
Nested Layout Managers?
ArrayList test not working?
load getResourceAsStrem
multithreading not working correctly