1

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?

0

1 Answer 1

1

On the termination of the loop, check if the last element is equal to the second last element.

Your updated code will be:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {

        byte[] pracArray = { 13, 13, 13, 3, 3, 3, 3, 1 };

        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];
        int i;
        for (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++;
            }
        }
        if (pracArray[i - 1] == pracArray[i]) {
            newArray[indexNum] = numLength;
            newArray[indexNum + 1] = pracArray[i];
            indexNum = (byte) (indexNum + 2);
            numLength = 1;
        } else {
            newArray[indexNum] = numLength;
            newArray[indexNum + 1] = pracArray[i];
        }

        System.out.println(Arrays.toString((pracArray)));
        System.out.println(Arrays.toString((newArray)));
    }
}

Output:

[13, 13, 13, 3, 3, 3, 3, 1]
[3, 13, 4, 3, 1, 1]

Output for byte[] pracArray = { 13, 13, 13, 3, 3, 3, 3 } will be as follows:

[13, 13, 13, 3, 3, 3, 3]
[3, 13, 4, 3]
Sign up to request clarification or add additional context in comments.

1 Comment

appreciate the help!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.