Creating an array from another array?

亡梦爱人 提交于 2020-06-16 17:14:38

问题


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?


回答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]


来源:https://stackoverflow.com/questions/60582538/creating-an-array-from-another-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!