New to java and am working with RLE encoding and I am trying to create a program that takes a byte array and then returns with another byte array but it takes the number of consecutive values in an array and then prints [ (number of times repeated),(value)]. Example, [ 13,13,13,3,3,3,3 ] would return [3,13,4,3]
import java.util.Arrays;
public class testing {
public static void main(String [] args) {
byte [] pracArray = {13,13,13,3,3,3,3};
int count = 1;
for (int i = 0; i <pracArray.length-1; i++)
{
if (pracArray[i] != pracArray[i+1])
{
count++;
}
}
byte numLength = 1;
byte indexNum = 0;
int newArraySize = count*2;
byte [] newArray = new byte [newArraySize];
for ( int i = 0; i < pracArray.length-1; i++)
{
if (pracArray[i] != pracArray[i+1] )
{
newArray[indexNum] = numLength;
newArray[indexNum+1] = pracArray[i];
indexNum = (byte) (indexNum + 2);
numLength = 1;
}
else
{
numLength++;
}
}
System.out.println(Arrays.toString((pracArray)));
System.out.println(Arrays.toString((newArray)));
}
}
I am aware of my problem, the last run of consecutive numbers does not get printed. If I change the array to [13,13,13,3,3,3,3,1] it will print [3,13,4,3,0,0], the last sequence of numbers is never recorded so the last two values of newArray never get filled. Any ideas on how I can change my if statement to include my last sequence of numbers?