Java中的Freeman链代码

时间:2017-12-06 16:45:08

标签: java arrays chain

好的,希望这更有意义。我有一个硬编码的阵列只有1和0。我正在尝试编写一个函数来读取每个元素以查看它是否为0或1.如果它是1,它将执行另一个函数,然后将该值更改为0,以便不再读取它。我打印它只是作为我将在稍后实现的其他功能的占位符。我无法通过findfirst1函数检查数组中的每个元素。我已经尝试将i和k的增量器放在代码流中的不同位置,但是我尝试过的任何东西都没有给我正确的输出。

public static void main(String[] args)
{
    int[][] testarray = {{1,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{1,0,0,0,0,0,0,1}};

    findfirst1(testarray);
}

public static void findfirst1(int[][] array1) 
{
    int value = 0;
    for(int i = 0; i < 6;i++)
    {
        for(int k = 0; k < 7;k++)
        {
            value = array1[i][k];
            if(value == 1)
            {
                System.out.println(value);
                array1[i][k] = 0;
            }
            else
            {
                System.out.println(value);
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

好的,所以在完全开始并从头开始编写之后我就明白了。 array.length一直都是正确的。我无法绕过它,因为我非常关注&#34;图像的概念。

编辑:我刚发现一个错误,它不会打印出array1的最后一行,所以我只添加了一行0并且它有效。

public class ChainCodeClass {

public static void main(String[] args)
{
    int[][] array1 = {{0,1,0,0,0,0,0,0},{0,1,0,0,0,1,0,0},{0,1,1,1,0,0,1,0},
            {0,0,0,1,1,0,0,1},{0,0,0,0,1,0,0,1},{0,0,0,0,0,1,0,0},
            {0,0,0,0,0,0,1,0}**,{0,0,0,0,0,0,0,0}**};

    int[][] array2 = {{0,0,0,0,0,0,0,0},{0,1,1,1,1,0,1,0},{0,0,0,0,1,0,1,0},
            {0,0,1,1,1,0,1,0},{0,0,1,0,0,0,1,0},{0,0,1,0,0,0,1,0},{0,0,0,0,0,0,1,1},
            {0,0,0,0,0,0,0,0}};

    System.out.print("First Image");
    print(array1);
    findfirst1(array1);
    print(array1);

    System.out.print("Second Image");
    print(array2);
    outline8(array2);
}

public static void findfirst1(int[][] array) 
{
    int value = 0;
    for(int i = 0; i < array.length; i++)
    {
        for(int k = 0; k < array.length; k++)
        {
            value = array[i][k];
            if(value == 1)
            {
                System.out.print(value + "  ");
                array[i][k] = 0;
            }
            else
            {
                System.out.println(value);
            }
        }
    }

}

public static void print(int[][] array)
{
for(int i = 0; i < array.length; i++) // print function for the array using incrementors
{
    System.out.print("\n");

    for(int k = 0; k < array.length; k++)
    {
        System.out.print(array[i][k] + "    ");
    }
}
    System.out.println();
}
 }