# 其他 - 在我的Java数组分类代码中找不到问题

``````    public static void zeroDistance (int [] a)
{
int counter = 0;
for (int i = 0; i < a.length; i++)
{
if (a[i] == 0)
counter = 0;
if (a[i] == 1)
{
counter++;
a[i] = counter;
}
}
counter = 0;
for (int i = a.length - 1; i >= 0; i--)
{
if (a[i] == 0)
counter = 0;
if (a[i] != 0)
{
counter++;
if (a[i] > counter)
a[i] = counter;
}
}
}
``````

``````int[] a={0,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1};
``````

``````zeroDistance method result :
0, 1, 2, 3, 3, 2, 1, 0, 1, 2, 3, 2, 1, 0, 1, 1
Should return :
0, 1, 2, 3, 3, 2, 1, 0, 1, 2, 3, 2, 1, 0, 1, 2
``````

Froggy

15
Philippe B. 2020-01-31 18:59

Your problem comes from the second loop. Since you start over from the end of the array, your second loop considers that you have encountered a 0 at the end of your array, which is obviously not the case here.

So according to your code in your second loop `if (a[i] > counter)` will be true, since `a[1]` is 2 and `counter` is 1 in your first iteration, that leading to your last member of your array being 1 in your result.

As other people said. Don't change values before you encounter a zero. Don't assume that the end of your array is a 0. If you want to fix your problem you can set `counter = a[a.length - 1];` before your second loop. But that is not very sexy, I think you can come up with another approach for your problem.

Hope this helps.

EDIT

``````public static void zeroDistance (int[] a)
{
List<Integer> zerosCoordinates = new ArrayList<>();

for (int i = 0; i < a.length; i++) {

if (a[i] == 0)
}

for(int i : zerosCoordinates) {

for (int j = i - 1, k = 1; j >= 0; j--, k++) {

if (a[j] == 0)
break;

if (a[j] != 0 && a[j] < k)
a[j] = k;
}
}

for (int i : zerosCoordinates) {

for (int j = i + 1, k = 1; j < a.length; j++, k++) {

if (a[j] == 0)
break;

if (a[j] != 0 && a[j] > k || j == a.length - 1)
a[j] = k;
}
}

for (int i = 0; i < a.length; i++)
System.out.print(a[i]);
}
``````