Search code examples
javaarrayschain

Freeman Chain Code in Java


Okay, hopefully this makes more sense. I have an array hard coded with only 1s and 0s. I am trying to write a function that reads each element to see if it is a 0 or 1. If it is a 1, it will execute another function and then change that 1 to a 0 so that it is not read again. I have it printing simply as a placeholder for the other function I will be implementing later. I'm having trouble getting the findfirst1 function to check every element in the array. I have tried putting the incrementors for i and k in different places within the flow of the code but nothing I have tried gets me the correct output.

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);
            }
        }
    }

}

Solution

  • Okay, so after starting completely over and writing it all from scratch I figured it out. The array.length was right all along. I had trouble wrapping my head around it because I was so focused on the idea of the "image".

    Edit: I just found an error where it wouldn't print the last line for array1, so I just added an extra row of 0s and it worked.

    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();
    }
     }