Getting a sequence of indexes



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm having a bit of a problem. The assignment is to take a picture, run it through an EdgeDetector, and get a 3 axis matrixprinter to draw the edge.



Most of it I got covered, but one major obstacle remains. When I run my picture through my Sobel and use a binary truncate it returns a 2D int array of 0's and 255's. Just an example of a test array for better understanding.



255 255 255 255 255 255 255 255
255 0 0 0 255 0 0 255
255 255 0 0 0 255 255 255
255 0 0 255 255 0 0 255
255 255 255 0 255 0 0 255
255 0 255 0 0 0 0 255
255 255 255 255 255 255 255 255


I'm then using a nested for loop to go through the array and give me an i coordinate, j coordinate and magnitude for each position in the array, like this. I'm using the StringBuilder, because I need to send a String to the PLC.



 public static void stringBuilder(int m)
// start method stringbuilder
StringBuilder sb = new StringBuilder();

for (int i = 0 ; i < m.length ; i++)
// start i for loop
for ( int j = 0 ; j < m[i].length ; j++)
// start j for loop
int magnitude = m[j][i];

/*
sb.append("(");
sb.append(i);
sb.append(",");
sb.append(j);
sb.append(")");
sb.append(" : ");
*/

if (magnitude == 0)

sb.append(magnitude);
sb.append(" ");

else

sb.append(magnitude);
sb.append(" ");

sb.append(" ");



// end j for loop
sb.append("n");

// end i for loop

System.out.println(sb);
// end method stringbuilder


I have also figured out how to print the elements of connected elements like this. This is by row, but have methods by column, diagonaly left and right. Again putting it in a StringBuilder.



 for (int i = 0; i < n.length ; i++) 
{
for (int j = 0; j < n[0].length ; j++)

int d = j + 1;
int e = j - 1;

if (d < n[0].length)





Now here's the problem. I would very much like to be able to send a string to the PLC containing only the coordinates where it is suppose to draw. In the example array I would be:




0,0 to 0,7 up



1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up



2,0 to 2,1 up ; 2,2 to 2,4 down ; 2,5 to 2,7 up



3,0 up ; 3,1 to 3,2 down ; 3,3 to 3,4 up ; 3,5 to 3,6 down ; 3,7 up



and so on and so forth.




In essence getting a sequense of element in the middle of the array.



The idea behind this is that the differens in j coordinates can be translated to a number of steps in a steppermotor.



I just can't figure out how to do it.










share|improve this question






























    0















    I'm having a bit of a problem. The assignment is to take a picture, run it through an EdgeDetector, and get a 3 axis matrixprinter to draw the edge.



    Most of it I got covered, but one major obstacle remains. When I run my picture through my Sobel and use a binary truncate it returns a 2D int array of 0's and 255's. Just an example of a test array for better understanding.



    255 255 255 255 255 255 255 255
    255 0 0 0 255 0 0 255
    255 255 0 0 0 255 255 255
    255 0 0 255 255 0 0 255
    255 255 255 0 255 0 0 255
    255 0 255 0 0 0 0 255
    255 255 255 255 255 255 255 255


    I'm then using a nested for loop to go through the array and give me an i coordinate, j coordinate and magnitude for each position in the array, like this. I'm using the StringBuilder, because I need to send a String to the PLC.



     public static void stringBuilder(int m)
    // start method stringbuilder
    StringBuilder sb = new StringBuilder();

    for (int i = 0 ; i < m.length ; i++)
    // start i for loop
    for ( int j = 0 ; j < m[i].length ; j++)
    // start j for loop
    int magnitude = m[j][i];

    /*
    sb.append("(");
    sb.append(i);
    sb.append(",");
    sb.append(j);
    sb.append(")");
    sb.append(" : ");
    */

    if (magnitude == 0)

    sb.append(magnitude);
    sb.append(" ");

    else

    sb.append(magnitude);
    sb.append(" ");

    sb.append(" ");



    // end j for loop
    sb.append("n");

    // end i for loop

    System.out.println(sb);
    // end method stringbuilder


    I have also figured out how to print the elements of connected elements like this. This is by row, but have methods by column, diagonaly left and right. Again putting it in a StringBuilder.



     for (int i = 0; i < n.length ; i++) 
    {
    for (int j = 0; j < n[0].length ; j++)

    int d = j + 1;
    int e = j - 1;

    if (d < n[0].length)





    Now here's the problem. I would very much like to be able to send a string to the PLC containing only the coordinates where it is suppose to draw. In the example array I would be:




    0,0 to 0,7 up



    1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up



    2,0 to 2,1 up ; 2,2 to 2,4 down ; 2,5 to 2,7 up



    3,0 up ; 3,1 to 3,2 down ; 3,3 to 3,4 up ; 3,5 to 3,6 down ; 3,7 up



    and so on and so forth.




    In essence getting a sequense of element in the middle of the array.



    The idea behind this is that the differens in j coordinates can be translated to a number of steps in a steppermotor.



    I just can't figure out how to do it.










    share|improve this question


























      0












      0








      0








      I'm having a bit of a problem. The assignment is to take a picture, run it through an EdgeDetector, and get a 3 axis matrixprinter to draw the edge.



      Most of it I got covered, but one major obstacle remains. When I run my picture through my Sobel and use a binary truncate it returns a 2D int array of 0's and 255's. Just an example of a test array for better understanding.



      255 255 255 255 255 255 255 255
      255 0 0 0 255 0 0 255
      255 255 0 0 0 255 255 255
      255 0 0 255 255 0 0 255
      255 255 255 0 255 0 0 255
      255 0 255 0 0 0 0 255
      255 255 255 255 255 255 255 255


      I'm then using a nested for loop to go through the array and give me an i coordinate, j coordinate and magnitude for each position in the array, like this. I'm using the StringBuilder, because I need to send a String to the PLC.



       public static void stringBuilder(int m)
      // start method stringbuilder
      StringBuilder sb = new StringBuilder();

      for (int i = 0 ; i < m.length ; i++)
      // start i for loop
      for ( int j = 0 ; j < m[i].length ; j++)
      // start j for loop
      int magnitude = m[j][i];

      /*
      sb.append("(");
      sb.append(i);
      sb.append(",");
      sb.append(j);
      sb.append(")");
      sb.append(" : ");
      */

      if (magnitude == 0)

      sb.append(magnitude);
      sb.append(" ");

      else

      sb.append(magnitude);
      sb.append(" ");

      sb.append(" ");



      // end j for loop
      sb.append("n");

      // end i for loop

      System.out.println(sb);
      // end method stringbuilder


      I have also figured out how to print the elements of connected elements like this. This is by row, but have methods by column, diagonaly left and right. Again putting it in a StringBuilder.



       for (int i = 0; i < n.length ; i++) 
      {
      for (int j = 0; j < n[0].length ; j++)

      int d = j + 1;
      int e = j - 1;

      if (d < n[0].length)





      Now here's the problem. I would very much like to be able to send a string to the PLC containing only the coordinates where it is suppose to draw. In the example array I would be:




      0,0 to 0,7 up



      1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up



      2,0 to 2,1 up ; 2,2 to 2,4 down ; 2,5 to 2,7 up



      3,0 up ; 3,1 to 3,2 down ; 3,3 to 3,4 up ; 3,5 to 3,6 down ; 3,7 up



      and so on and so forth.




      In essence getting a sequense of element in the middle of the array.



      The idea behind this is that the differens in j coordinates can be translated to a number of steps in a steppermotor.



      I just can't figure out how to do it.










      share|improve this question
















      I'm having a bit of a problem. The assignment is to take a picture, run it through an EdgeDetector, and get a 3 axis matrixprinter to draw the edge.



      Most of it I got covered, but one major obstacle remains. When I run my picture through my Sobel and use a binary truncate it returns a 2D int array of 0's and 255's. Just an example of a test array for better understanding.



      255 255 255 255 255 255 255 255
      255 0 0 0 255 0 0 255
      255 255 0 0 0 255 255 255
      255 0 0 255 255 0 0 255
      255 255 255 0 255 0 0 255
      255 0 255 0 0 0 0 255
      255 255 255 255 255 255 255 255


      I'm then using a nested for loop to go through the array and give me an i coordinate, j coordinate and magnitude for each position in the array, like this. I'm using the StringBuilder, because I need to send a String to the PLC.



       public static void stringBuilder(int m)
      // start method stringbuilder
      StringBuilder sb = new StringBuilder();

      for (int i = 0 ; i < m.length ; i++)
      // start i for loop
      for ( int j = 0 ; j < m[i].length ; j++)
      // start j for loop
      int magnitude = m[j][i];

      /*
      sb.append("(");
      sb.append(i);
      sb.append(",");
      sb.append(j);
      sb.append(")");
      sb.append(" : ");
      */

      if (magnitude == 0)

      sb.append(magnitude);
      sb.append(" ");

      else

      sb.append(magnitude);
      sb.append(" ");

      sb.append(" ");



      // end j for loop
      sb.append("n");

      // end i for loop

      System.out.println(sb);
      // end method stringbuilder


      I have also figured out how to print the elements of connected elements like this. This is by row, but have methods by column, diagonaly left and right. Again putting it in a StringBuilder.



       for (int i = 0; i < n.length ; i++) 
      {
      for (int j = 0; j < n[0].length ; j++)

      int d = j + 1;
      int e = j - 1;

      if (d < n[0].length)





      Now here's the problem. I would very much like to be able to send a string to the PLC containing only the coordinates where it is suppose to draw. In the example array I would be:




      0,0 to 0,7 up



      1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up



      2,0 to 2,1 up ; 2,2 to 2,4 down ; 2,5 to 2,7 up



      3,0 up ; 3,1 to 3,2 down ; 3,3 to 3,4 up ; 3,5 to 3,6 down ; 3,7 up



      and so on and so forth.




      In essence getting a sequense of element in the middle of the array.



      The idea behind this is that the differens in j coordinates can be translated to a number of steps in a steppermotor.



      I just can't figure out how to do it.







      java arrays multidimensional-array






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 9:20









      Andrea

      4,54012144




      4,54012144










      asked Nov 15 '18 at 7:57









      Peter Lindhøj TuxenPeter Lindhøj Tuxen

      102




      102






















          2 Answers
          2






          active

          oldest

          votes


















          0














          Reading line by line, and checking when the value changes, you can easily manage it.



          Here's an example:



          public static void main(String args) 
          int array =
          255, 255, 255, 255, 255, 255, 255, 255 ,
          255, 0, 0, 0, 255, 0, 0, 255 ,
          255, 255, 0, 0, 0, 255, 255, 255 ,
          255, 0, 0, 255, 255, 0, 0, 255 ,
          255, 255, 255, 0, 255, 0, 0, 255 ,
          255, 0, 255, 0, 0, 0, 0, 255 ,
          255, 255, 255, 255, 255, 255, 255, 255
          ;
          StringBuilder result = new StringBuilder();
          int currentValue;
          int startIndex;
          for (int i = 0; i < array.length; i++)
          if (array[i].length == 0)
          continue;

          currentValue = array[i][0];
          startIndex = 0;
          for (int j = 1; j < array[i].length; j++)
          if (currentValue != array[i][j])
          updateResult(result, currentValue, i, startIndex, j - 1);
          currentValue = array[i][j];
          startIndex = j;


          updateResult(result, currentValue, i, startIndex, array[i].length - 1);
          result.append("n");

          System.out.println(result.toString());


          private static void updateResult(StringBuilder result, int value, int row, int startIndex, int endIndex)
          if (startIndex == endIndex)
          addCoordinates(result, row, startIndex);
          else
          addCoordinates(result, row, startIndex);
          result.append(" to ");
          addCoordinates(result, row, endIndex);

          result.append(" ");
          switch(value)
          case 255:
          result.append("up");
          break;
          case 0:
          result.append("down");
          break;
          default:
          System.err.println("Unrecognized value " + value);

          result.append("; ");


          private static void addCoordinates(StringBuilder result, int row, int column)
          result.append(row);
          result.append(",");
          result.append(column);



          For your input array, the result will be:



          0,0 to 0,7 up; 
          1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up;
          2,0 to 2,1 up; 2,2 to 2,4 down; 2,5 to 2,7 up;
          3,0 up; 3,1 to 3,2 down; 3,3 to 3,4 up; 3,5 to 3,6 down; 3,7 up;
          4,0 to 4,2 up; 4,3 down; 4,4 up; 4,5 to 4,6 down; 4,7 up;
          5,0 up; 5,1 down; 5,2 up; 5,3 to 5,6 down; 5,7 up;
          6,0 to 6,7 up;





          share|improve this answer























          • Fantastic. Thanks for answers. Will give it a try.

            – Peter Lindhøj Tuxen
            Nov 15 '18 at 9:18


















          0














          Try out this code:



          // String formatting
          public static final String FORMAT_TO_UP = "%d,%d to %d,%d up";
          public static final String FORMAT_TO_DOWN = "%d,%d to %d,%d down";

          public static final String FORMAT_UP = "%d,%d up";
          public static final String FORMAT_DOWN = "%d,%d down";

          public static void stringBuilder(int m)
          // start method stringbuilder
          StringBuilder sb = new StringBuilder();

          for(int i = 0; i < m.length; i++)
          // start i for loop

          int upStart = 0;
          int downStart = 0;

          boolean hasPreviousUp = false;
          boolean hasPreviousDown = false;

          final int rowSize = m[i].length;
          for(int j = 0; j < rowSize; j++)
          // start j for loop
          int magnitude = m[i][j];

          // Reached a down
          if(magnitude == 0)

          // Handle the change from up to down
          if(hasPreviousUp)

          // If range is only 1
          if(upStart == j - 1)

          sb.append(String.format(FORMAT_UP, i, j - 1));

          else

          sb.append(String.format(FORMAT_TO_UP, i, upStart, i, j - 1));

          hasPreviousUp = false;
          sb.append("; ");


          // Don't reset the start value if there was no change of up/down
          if(!hasPreviousDown)

          hasPreviousDown = true;
          downStart = j;


          // Reached an up
          else

          // Handle the change from down to up
          if(hasPreviousDown)

          // If range is only 1
          if(downStart == j - 1)

          sb.append(String.format(FORMAT_DOWN, i, j - 1));

          else

          sb.append(String.format(FORMAT_TO_DOWN, i, downStart, i, j - 1));

          hasPreviousDown = false;
          sb.append("; ");


          // Don't reset the start value if there was no change of up/down
          if(!hasPreviousUp)

          hasPreviousUp = true;
          upStart = j;



          // end j for loop

          // Handle the last up/down range
          if(hasPreviousUp)

          if(upStart == rowSize - 1)

          sb.append(String.format(FORMAT_UP, i, rowSize - 1));

          else

          sb.append(String.format(FORMAT_TO_UP, i, upStart, i, rowSize - 1));


          else if(hasPreviousDown)

          if(downStart == rowSize - 1)

          sb.append(String.format(FORMAT_DOWN, i, rowSize - 1));

          else

          sb.append(String.format(FORMAT_TO_DOWN, i, downStart, i, rowSize - 1));


          sb.append("n");

          // end i for loop

          System.out.println(sb);
          // end method stringbuilder


          Result:



          0,0 to 0,7 up
          1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up
          2,0 to 2,1 up; 2,2 to 2,4 down; 2,5 to 2,7 up
          3,0 up; 3,1 to 3,2 down; 3,3 to 3,4 up; 3,5 to 3,6 down; 3,7 up
          4,0 to 4,2 up; 4,3 down; 4,4 up; 4,5 to 4,6 down; 4,7 up
          5,0 up; 5,1 down; 5,2 up; 5,3 to 5,6 down; 5,7 up
          6,0 to 6,7 up





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314751%2fgetting-a-sequence-of-indexes%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Reading line by line, and checking when the value changes, you can easily manage it.



            Here's an example:



            public static void main(String args) 
            int array =
            255, 255, 255, 255, 255, 255, 255, 255 ,
            255, 0, 0, 0, 255, 0, 0, 255 ,
            255, 255, 0, 0, 0, 255, 255, 255 ,
            255, 0, 0, 255, 255, 0, 0, 255 ,
            255, 255, 255, 0, 255, 0, 0, 255 ,
            255, 0, 255, 0, 0, 0, 0, 255 ,
            255, 255, 255, 255, 255, 255, 255, 255
            ;
            StringBuilder result = new StringBuilder();
            int currentValue;
            int startIndex;
            for (int i = 0; i < array.length; i++)
            if (array[i].length == 0)
            continue;

            currentValue = array[i][0];
            startIndex = 0;
            for (int j = 1; j < array[i].length; j++)
            if (currentValue != array[i][j])
            updateResult(result, currentValue, i, startIndex, j - 1);
            currentValue = array[i][j];
            startIndex = j;


            updateResult(result, currentValue, i, startIndex, array[i].length - 1);
            result.append("n");

            System.out.println(result.toString());


            private static void updateResult(StringBuilder result, int value, int row, int startIndex, int endIndex)
            if (startIndex == endIndex)
            addCoordinates(result, row, startIndex);
            else
            addCoordinates(result, row, startIndex);
            result.append(" to ");
            addCoordinates(result, row, endIndex);

            result.append(" ");
            switch(value)
            case 255:
            result.append("up");
            break;
            case 0:
            result.append("down");
            break;
            default:
            System.err.println("Unrecognized value " + value);

            result.append("; ");


            private static void addCoordinates(StringBuilder result, int row, int column)
            result.append(row);
            result.append(",");
            result.append(column);



            For your input array, the result will be:



            0,0 to 0,7 up; 
            1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up;
            2,0 to 2,1 up; 2,2 to 2,4 down; 2,5 to 2,7 up;
            3,0 up; 3,1 to 3,2 down; 3,3 to 3,4 up; 3,5 to 3,6 down; 3,7 up;
            4,0 to 4,2 up; 4,3 down; 4,4 up; 4,5 to 4,6 down; 4,7 up;
            5,0 up; 5,1 down; 5,2 up; 5,3 to 5,6 down; 5,7 up;
            6,0 to 6,7 up;





            share|improve this answer























            • Fantastic. Thanks for answers. Will give it a try.

              – Peter Lindhøj Tuxen
              Nov 15 '18 at 9:18















            0














            Reading line by line, and checking when the value changes, you can easily manage it.



            Here's an example:



            public static void main(String args) 
            int array =
            255, 255, 255, 255, 255, 255, 255, 255 ,
            255, 0, 0, 0, 255, 0, 0, 255 ,
            255, 255, 0, 0, 0, 255, 255, 255 ,
            255, 0, 0, 255, 255, 0, 0, 255 ,
            255, 255, 255, 0, 255, 0, 0, 255 ,
            255, 0, 255, 0, 0, 0, 0, 255 ,
            255, 255, 255, 255, 255, 255, 255, 255
            ;
            StringBuilder result = new StringBuilder();
            int currentValue;
            int startIndex;
            for (int i = 0; i < array.length; i++)
            if (array[i].length == 0)
            continue;

            currentValue = array[i][0];
            startIndex = 0;
            for (int j = 1; j < array[i].length; j++)
            if (currentValue != array[i][j])
            updateResult(result, currentValue, i, startIndex, j - 1);
            currentValue = array[i][j];
            startIndex = j;


            updateResult(result, currentValue, i, startIndex, array[i].length - 1);
            result.append("n");

            System.out.println(result.toString());


            private static void updateResult(StringBuilder result, int value, int row, int startIndex, int endIndex)
            if (startIndex == endIndex)
            addCoordinates(result, row, startIndex);
            else
            addCoordinates(result, row, startIndex);
            result.append(" to ");
            addCoordinates(result, row, endIndex);

            result.append(" ");
            switch(value)
            case 255:
            result.append("up");
            break;
            case 0:
            result.append("down");
            break;
            default:
            System.err.println("Unrecognized value " + value);

            result.append("; ");


            private static void addCoordinates(StringBuilder result, int row, int column)
            result.append(row);
            result.append(",");
            result.append(column);



            For your input array, the result will be:



            0,0 to 0,7 up; 
            1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up;
            2,0 to 2,1 up; 2,2 to 2,4 down; 2,5 to 2,7 up;
            3,0 up; 3,1 to 3,2 down; 3,3 to 3,4 up; 3,5 to 3,6 down; 3,7 up;
            4,0 to 4,2 up; 4,3 down; 4,4 up; 4,5 to 4,6 down; 4,7 up;
            5,0 up; 5,1 down; 5,2 up; 5,3 to 5,6 down; 5,7 up;
            6,0 to 6,7 up;





            share|improve this answer























            • Fantastic. Thanks for answers. Will give it a try.

              – Peter Lindhøj Tuxen
              Nov 15 '18 at 9:18













            0












            0








            0







            Reading line by line, and checking when the value changes, you can easily manage it.



            Here's an example:



            public static void main(String args) 
            int array =
            255, 255, 255, 255, 255, 255, 255, 255 ,
            255, 0, 0, 0, 255, 0, 0, 255 ,
            255, 255, 0, 0, 0, 255, 255, 255 ,
            255, 0, 0, 255, 255, 0, 0, 255 ,
            255, 255, 255, 0, 255, 0, 0, 255 ,
            255, 0, 255, 0, 0, 0, 0, 255 ,
            255, 255, 255, 255, 255, 255, 255, 255
            ;
            StringBuilder result = new StringBuilder();
            int currentValue;
            int startIndex;
            for (int i = 0; i < array.length; i++)
            if (array[i].length == 0)
            continue;

            currentValue = array[i][0];
            startIndex = 0;
            for (int j = 1; j < array[i].length; j++)
            if (currentValue != array[i][j])
            updateResult(result, currentValue, i, startIndex, j - 1);
            currentValue = array[i][j];
            startIndex = j;


            updateResult(result, currentValue, i, startIndex, array[i].length - 1);
            result.append("n");

            System.out.println(result.toString());


            private static void updateResult(StringBuilder result, int value, int row, int startIndex, int endIndex)
            if (startIndex == endIndex)
            addCoordinates(result, row, startIndex);
            else
            addCoordinates(result, row, startIndex);
            result.append(" to ");
            addCoordinates(result, row, endIndex);

            result.append(" ");
            switch(value)
            case 255:
            result.append("up");
            break;
            case 0:
            result.append("down");
            break;
            default:
            System.err.println("Unrecognized value " + value);

            result.append("; ");


            private static void addCoordinates(StringBuilder result, int row, int column)
            result.append(row);
            result.append(",");
            result.append(column);



            For your input array, the result will be:



            0,0 to 0,7 up; 
            1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up;
            2,0 to 2,1 up; 2,2 to 2,4 down; 2,5 to 2,7 up;
            3,0 up; 3,1 to 3,2 down; 3,3 to 3,4 up; 3,5 to 3,6 down; 3,7 up;
            4,0 to 4,2 up; 4,3 down; 4,4 up; 4,5 to 4,6 down; 4,7 up;
            5,0 up; 5,1 down; 5,2 up; 5,3 to 5,6 down; 5,7 up;
            6,0 to 6,7 up;





            share|improve this answer













            Reading line by line, and checking when the value changes, you can easily manage it.



            Here's an example:



            public static void main(String args) 
            int array =
            255, 255, 255, 255, 255, 255, 255, 255 ,
            255, 0, 0, 0, 255, 0, 0, 255 ,
            255, 255, 0, 0, 0, 255, 255, 255 ,
            255, 0, 0, 255, 255, 0, 0, 255 ,
            255, 255, 255, 0, 255, 0, 0, 255 ,
            255, 0, 255, 0, 0, 0, 0, 255 ,
            255, 255, 255, 255, 255, 255, 255, 255
            ;
            StringBuilder result = new StringBuilder();
            int currentValue;
            int startIndex;
            for (int i = 0; i < array.length; i++)
            if (array[i].length == 0)
            continue;

            currentValue = array[i][0];
            startIndex = 0;
            for (int j = 1; j < array[i].length; j++)
            if (currentValue != array[i][j])
            updateResult(result, currentValue, i, startIndex, j - 1);
            currentValue = array[i][j];
            startIndex = j;


            updateResult(result, currentValue, i, startIndex, array[i].length - 1);
            result.append("n");

            System.out.println(result.toString());


            private static void updateResult(StringBuilder result, int value, int row, int startIndex, int endIndex)
            if (startIndex == endIndex)
            addCoordinates(result, row, startIndex);
            else
            addCoordinates(result, row, startIndex);
            result.append(" to ");
            addCoordinates(result, row, endIndex);

            result.append(" ");
            switch(value)
            case 255:
            result.append("up");
            break;
            case 0:
            result.append("down");
            break;
            default:
            System.err.println("Unrecognized value " + value);

            result.append("; ");


            private static void addCoordinates(StringBuilder result, int row, int column)
            result.append(row);
            result.append(",");
            result.append(column);



            For your input array, the result will be:



            0,0 to 0,7 up; 
            1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up;
            2,0 to 2,1 up; 2,2 to 2,4 down; 2,5 to 2,7 up;
            3,0 up; 3,1 to 3,2 down; 3,3 to 3,4 up; 3,5 to 3,6 down; 3,7 up;
            4,0 to 4,2 up; 4,3 down; 4,4 up; 4,5 to 4,6 down; 4,7 up;
            5,0 up; 5,1 down; 5,2 up; 5,3 to 5,6 down; 5,7 up;
            6,0 to 6,7 up;






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 8:57









            raven1981raven1981

            9401217




            9401217












            • Fantastic. Thanks for answers. Will give it a try.

              – Peter Lindhøj Tuxen
              Nov 15 '18 at 9:18

















            • Fantastic. Thanks for answers. Will give it a try.

              – Peter Lindhøj Tuxen
              Nov 15 '18 at 9:18
















            Fantastic. Thanks for answers. Will give it a try.

            – Peter Lindhøj Tuxen
            Nov 15 '18 at 9:18





            Fantastic. Thanks for answers. Will give it a try.

            – Peter Lindhøj Tuxen
            Nov 15 '18 at 9:18













            0














            Try out this code:



            // String formatting
            public static final String FORMAT_TO_UP = "%d,%d to %d,%d up";
            public static final String FORMAT_TO_DOWN = "%d,%d to %d,%d down";

            public static final String FORMAT_UP = "%d,%d up";
            public static final String FORMAT_DOWN = "%d,%d down";

            public static void stringBuilder(int m)
            // start method stringbuilder
            StringBuilder sb = new StringBuilder();

            for(int i = 0; i < m.length; i++)
            // start i for loop

            int upStart = 0;
            int downStart = 0;

            boolean hasPreviousUp = false;
            boolean hasPreviousDown = false;

            final int rowSize = m[i].length;
            for(int j = 0; j < rowSize; j++)
            // start j for loop
            int magnitude = m[i][j];

            // Reached a down
            if(magnitude == 0)

            // Handle the change from up to down
            if(hasPreviousUp)

            // If range is only 1
            if(upStart == j - 1)

            sb.append(String.format(FORMAT_UP, i, j - 1));

            else

            sb.append(String.format(FORMAT_TO_UP, i, upStart, i, j - 1));

            hasPreviousUp = false;
            sb.append("; ");


            // Don't reset the start value if there was no change of up/down
            if(!hasPreviousDown)

            hasPreviousDown = true;
            downStart = j;


            // Reached an up
            else

            // Handle the change from down to up
            if(hasPreviousDown)

            // If range is only 1
            if(downStart == j - 1)

            sb.append(String.format(FORMAT_DOWN, i, j - 1));

            else

            sb.append(String.format(FORMAT_TO_DOWN, i, downStart, i, j - 1));

            hasPreviousDown = false;
            sb.append("; ");


            // Don't reset the start value if there was no change of up/down
            if(!hasPreviousUp)

            hasPreviousUp = true;
            upStart = j;



            // end j for loop

            // Handle the last up/down range
            if(hasPreviousUp)

            if(upStart == rowSize - 1)

            sb.append(String.format(FORMAT_UP, i, rowSize - 1));

            else

            sb.append(String.format(FORMAT_TO_UP, i, upStart, i, rowSize - 1));


            else if(hasPreviousDown)

            if(downStart == rowSize - 1)

            sb.append(String.format(FORMAT_DOWN, i, rowSize - 1));

            else

            sb.append(String.format(FORMAT_TO_DOWN, i, downStart, i, rowSize - 1));


            sb.append("n");

            // end i for loop

            System.out.println(sb);
            // end method stringbuilder


            Result:



            0,0 to 0,7 up
            1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up
            2,0 to 2,1 up; 2,2 to 2,4 down; 2,5 to 2,7 up
            3,0 up; 3,1 to 3,2 down; 3,3 to 3,4 up; 3,5 to 3,6 down; 3,7 up
            4,0 to 4,2 up; 4,3 down; 4,4 up; 4,5 to 4,6 down; 4,7 up
            5,0 up; 5,1 down; 5,2 up; 5,3 to 5,6 down; 5,7 up
            6,0 to 6,7 up





            share|improve this answer



























              0














              Try out this code:



              // String formatting
              public static final String FORMAT_TO_UP = "%d,%d to %d,%d up";
              public static final String FORMAT_TO_DOWN = "%d,%d to %d,%d down";

              public static final String FORMAT_UP = "%d,%d up";
              public static final String FORMAT_DOWN = "%d,%d down";

              public static void stringBuilder(int m)
              // start method stringbuilder
              StringBuilder sb = new StringBuilder();

              for(int i = 0; i < m.length; i++)
              // start i for loop

              int upStart = 0;
              int downStart = 0;

              boolean hasPreviousUp = false;
              boolean hasPreviousDown = false;

              final int rowSize = m[i].length;
              for(int j = 0; j < rowSize; j++)
              // start j for loop
              int magnitude = m[i][j];

              // Reached a down
              if(magnitude == 0)

              // Handle the change from up to down
              if(hasPreviousUp)

              // If range is only 1
              if(upStart == j - 1)

              sb.append(String.format(FORMAT_UP, i, j - 1));

              else

              sb.append(String.format(FORMAT_TO_UP, i, upStart, i, j - 1));

              hasPreviousUp = false;
              sb.append("; ");


              // Don't reset the start value if there was no change of up/down
              if(!hasPreviousDown)

              hasPreviousDown = true;
              downStart = j;


              // Reached an up
              else

              // Handle the change from down to up
              if(hasPreviousDown)

              // If range is only 1
              if(downStart == j - 1)

              sb.append(String.format(FORMAT_DOWN, i, j - 1));

              else

              sb.append(String.format(FORMAT_TO_DOWN, i, downStart, i, j - 1));

              hasPreviousDown = false;
              sb.append("; ");


              // Don't reset the start value if there was no change of up/down
              if(!hasPreviousUp)

              hasPreviousUp = true;
              upStart = j;



              // end j for loop

              // Handle the last up/down range
              if(hasPreviousUp)

              if(upStart == rowSize - 1)

              sb.append(String.format(FORMAT_UP, i, rowSize - 1));

              else

              sb.append(String.format(FORMAT_TO_UP, i, upStart, i, rowSize - 1));


              else if(hasPreviousDown)

              if(downStart == rowSize - 1)

              sb.append(String.format(FORMAT_DOWN, i, rowSize - 1));

              else

              sb.append(String.format(FORMAT_TO_DOWN, i, downStart, i, rowSize - 1));


              sb.append("n");

              // end i for loop

              System.out.println(sb);
              // end method stringbuilder


              Result:



              0,0 to 0,7 up
              1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up
              2,0 to 2,1 up; 2,2 to 2,4 down; 2,5 to 2,7 up
              3,0 up; 3,1 to 3,2 down; 3,3 to 3,4 up; 3,5 to 3,6 down; 3,7 up
              4,0 to 4,2 up; 4,3 down; 4,4 up; 4,5 to 4,6 down; 4,7 up
              5,0 up; 5,1 down; 5,2 up; 5,3 to 5,6 down; 5,7 up
              6,0 to 6,7 up





              share|improve this answer

























                0












                0








                0







                Try out this code:



                // String formatting
                public static final String FORMAT_TO_UP = "%d,%d to %d,%d up";
                public static final String FORMAT_TO_DOWN = "%d,%d to %d,%d down";

                public static final String FORMAT_UP = "%d,%d up";
                public static final String FORMAT_DOWN = "%d,%d down";

                public static void stringBuilder(int m)
                // start method stringbuilder
                StringBuilder sb = new StringBuilder();

                for(int i = 0; i < m.length; i++)
                // start i for loop

                int upStart = 0;
                int downStart = 0;

                boolean hasPreviousUp = false;
                boolean hasPreviousDown = false;

                final int rowSize = m[i].length;
                for(int j = 0; j < rowSize; j++)
                // start j for loop
                int magnitude = m[i][j];

                // Reached a down
                if(magnitude == 0)

                // Handle the change from up to down
                if(hasPreviousUp)

                // If range is only 1
                if(upStart == j - 1)

                sb.append(String.format(FORMAT_UP, i, j - 1));

                else

                sb.append(String.format(FORMAT_TO_UP, i, upStart, i, j - 1));

                hasPreviousUp = false;
                sb.append("; ");


                // Don't reset the start value if there was no change of up/down
                if(!hasPreviousDown)

                hasPreviousDown = true;
                downStart = j;


                // Reached an up
                else

                // Handle the change from down to up
                if(hasPreviousDown)

                // If range is only 1
                if(downStart == j - 1)

                sb.append(String.format(FORMAT_DOWN, i, j - 1));

                else

                sb.append(String.format(FORMAT_TO_DOWN, i, downStart, i, j - 1));

                hasPreviousDown = false;
                sb.append("; ");


                // Don't reset the start value if there was no change of up/down
                if(!hasPreviousUp)

                hasPreviousUp = true;
                upStart = j;



                // end j for loop

                // Handle the last up/down range
                if(hasPreviousUp)

                if(upStart == rowSize - 1)

                sb.append(String.format(FORMAT_UP, i, rowSize - 1));

                else

                sb.append(String.format(FORMAT_TO_UP, i, upStart, i, rowSize - 1));


                else if(hasPreviousDown)

                if(downStart == rowSize - 1)

                sb.append(String.format(FORMAT_DOWN, i, rowSize - 1));

                else

                sb.append(String.format(FORMAT_TO_DOWN, i, downStart, i, rowSize - 1));


                sb.append("n");

                // end i for loop

                System.out.println(sb);
                // end method stringbuilder


                Result:



                0,0 to 0,7 up
                1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up
                2,0 to 2,1 up; 2,2 to 2,4 down; 2,5 to 2,7 up
                3,0 up; 3,1 to 3,2 down; 3,3 to 3,4 up; 3,5 to 3,6 down; 3,7 up
                4,0 to 4,2 up; 4,3 down; 4,4 up; 4,5 to 4,6 down; 4,7 up
                5,0 up; 5,1 down; 5,2 up; 5,3 to 5,6 down; 5,7 up
                6,0 to 6,7 up





                share|improve this answer













                Try out this code:



                // String formatting
                public static final String FORMAT_TO_UP = "%d,%d to %d,%d up";
                public static final String FORMAT_TO_DOWN = "%d,%d to %d,%d down";

                public static final String FORMAT_UP = "%d,%d up";
                public static final String FORMAT_DOWN = "%d,%d down";

                public static void stringBuilder(int m)
                // start method stringbuilder
                StringBuilder sb = new StringBuilder();

                for(int i = 0; i < m.length; i++)
                // start i for loop

                int upStart = 0;
                int downStart = 0;

                boolean hasPreviousUp = false;
                boolean hasPreviousDown = false;

                final int rowSize = m[i].length;
                for(int j = 0; j < rowSize; j++)
                // start j for loop
                int magnitude = m[i][j];

                // Reached a down
                if(magnitude == 0)

                // Handle the change from up to down
                if(hasPreviousUp)

                // If range is only 1
                if(upStart == j - 1)

                sb.append(String.format(FORMAT_UP, i, j - 1));

                else

                sb.append(String.format(FORMAT_TO_UP, i, upStart, i, j - 1));

                hasPreviousUp = false;
                sb.append("; ");


                // Don't reset the start value if there was no change of up/down
                if(!hasPreviousDown)

                hasPreviousDown = true;
                downStart = j;


                // Reached an up
                else

                // Handle the change from down to up
                if(hasPreviousDown)

                // If range is only 1
                if(downStart == j - 1)

                sb.append(String.format(FORMAT_DOWN, i, j - 1));

                else

                sb.append(String.format(FORMAT_TO_DOWN, i, downStart, i, j - 1));

                hasPreviousDown = false;
                sb.append("; ");


                // Don't reset the start value if there was no change of up/down
                if(!hasPreviousUp)

                hasPreviousUp = true;
                upStart = j;



                // end j for loop

                // Handle the last up/down range
                if(hasPreviousUp)

                if(upStart == rowSize - 1)

                sb.append(String.format(FORMAT_UP, i, rowSize - 1));

                else

                sb.append(String.format(FORMAT_TO_UP, i, upStart, i, rowSize - 1));


                else if(hasPreviousDown)

                if(downStart == rowSize - 1)

                sb.append(String.format(FORMAT_DOWN, i, rowSize - 1));

                else

                sb.append(String.format(FORMAT_TO_DOWN, i, downStart, i, rowSize - 1));


                sb.append("n");

                // end i for loop

                System.out.println(sb);
                // end method stringbuilder


                Result:



                0,0 to 0,7 up
                1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up
                2,0 to 2,1 up; 2,2 to 2,4 down; 2,5 to 2,7 up
                3,0 up; 3,1 to 3,2 down; 3,3 to 3,4 up; 3,5 to 3,6 down; 3,7 up
                4,0 to 4,2 up; 4,3 down; 4,4 up; 4,5 to 4,6 down; 4,7 up
                5,0 up; 5,1 down; 5,2 up; 5,3 to 5,6 down; 5,7 up
                6,0 to 6,7 up






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 9:11









                Justin TayJustin Tay

                744




                744



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314751%2fgetting-a-sequence-of-indexes%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                    Syphilis

                    Darth Vader #20