Shareef Uddin wrote:
I will try to use stringTokenizer API.
Is that ok?
well...yes...but that is far from complete. Your original question was akin to "how do I build a skyscraper?" and this is like saying "I will try to use a screwdriver". while a screwdriver may be necesary, there is a LOT more going on here.
What I might do is
create a second 2d int array the same size as your input.
create a counter to keep track of which 'group' of 1's you are looking for
initialize each element to '-1'.
find the first element in this array that has a value of -1, which means I haven't looked at it yet.
if the corresponding element in the input array is 0, set it to 0
else
set the value in the new 2d array to the value of your counter
find all 1's next to this one, and set the corresponding elements to the counter value
find all neighbors of those elements, etc
in this way, you build a second array that keeps track of which 'grouping' a 1-value belongs to. Then, you can go through, count how many elements are in the 1-group, the 2-group, etc.
given your initial input, my new array would look something like
11000
0
1100
00
10
2
3000
2
0
30
22