Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何根据前面索引中的值将字节值重复添加到字节数组x次?_Java_Arrays_Encoding_Decoding - Fatal编程技术网

Java 如何根据前面索引中的值将字节值重复添加到字节数组x次?

Java 如何根据前面索引中的值将字节值重复添加到字节数组x次?,java,arrays,encoding,decoding,Java,Arrays,Encoding,Decoding,我正在用Java开发一个RLE编码项目,需要完成8个方法。其中之一涉及获取表示压缩的RLE字节数组(例如:{3,15,6,4})的字节数组,并将其解压缩到其原始值({15,15,15,4,4,4,4,4})。我将如何将解决方案循环到压缩数组中的第二个值加x次的位置(例如,将值15加三次到新字节数组,或将值4加六次到新数组) 我尝试创建两个嵌套的for循环,我想知道是否可以将每个奇数索引处的字节值转换为int,并让每个偶数(在本例中)根据前面的奇数索引数的int重复将该次数附加到字节数组中。我不知

我正在用Java开发一个RLE编码项目,需要完成8个方法。其中之一涉及获取表示压缩的RLE字节数组(例如:{3,15,6,4})的字节数组,并将其解压缩到其原始值({15,15,15,4,4,4,4,4})。我将如何将解决方案循环到压缩数组中的第二个值加x次的位置(例如,将值15加三次到新字节数组,或将值4加六次到新数组)

我尝试创建两个嵌套的for循环,我想知道是否可以将每个奇数索引处的字节值转换为int,并让每个偶数(在本例中)根据前面的奇数索引数的int重复将该次数附加到字节数组中。我不知道我该如何实现这个方法。我已经创建了一个对RLE字节数组进行编码的方法(将{15,15,15,4,4,4,4}压缩到返回{3,15,6,4}的位置)。基本上我必须做相反的事情,把它带回到前一个字节数组

RLE数据的编码方法


        int countRunResult = countRuns(flatData);
        ByteArrayOutputStream returnedEncoded = new ByteArrayOutputStream();
        for(int i = 0; i < flatData.length; i++){
            int runLength = 1;
            while(i+1 < flatData.length && flatData[i] == flatData[i+1]){
                runLength++;
                i++;
            }
            returnedEncoded.write((byte)runLength);
            returnedEncoded.write((byte)flatData[i]);
        }
        return returnedEncoded.toByteArray();
    }
        int initialLength = getDecodedLength(rleData);
        byte[] returnDecoded = new byte[initialLength];
           for(int i = 0; i < initialLength; i++){
                for(int j = 1; j < initialLength; j+=2){

                    returnDecoded[i] = rleData[j];
                }

            }

        return returnDecoded;   
    }

int countRunResult=countRuns(flatData);
ByteArrayOutputStream returnedEncoded=新建ByteArrayOutputStream();
对于(int i=0;i
到目前为止,我对RLE数据的解码


        int countRunResult = countRuns(flatData);
        ByteArrayOutputStream returnedEncoded = new ByteArrayOutputStream();
        for(int i = 0; i < flatData.length; i++){
            int runLength = 1;
            while(i+1 < flatData.length && flatData[i] == flatData[i+1]){
                runLength++;
                i++;
            }
            returnedEncoded.write((byte)runLength);
            returnedEncoded.write((byte)flatData[i]);
        }
        return returnedEncoded.toByteArray();
    }
        int initialLength = getDecodedLength(rleData);
        byte[] returnDecoded = new byte[initialLength];
           for(int i = 0; i < initialLength; i++){
                for(int j = 1; j < initialLength; j+=2){

                    returnDecoded[i] = rleData[j];
                }

            }

        return returnDecoded;   
    }
int initialLength=getDecodedLength(rleData);
字节[]返回解码=新字节[初始长度];
对于(int i=0;i
在本例中,我期望{15,15,15,4,4,4,4,4},但我收到了一条错误越界消息,其中说:

线程“main”java.lang.ArrayIndexOutOfBoundsException中出现异常:长度为4的索引>5超出边界 在RleProgram.decodeRle(RleProgram.java:61) 位于RleProgram.main(RleProgram.java:99)


看起来
getDecodedLength
返回的是编码长度,而不是解码长度,因此数组的长度是4而不是9。@Jannik谢谢。它成功地返回了一个值,尽管出于某种原因,当我在测试字节数组时,将其打印为字符串时,它返回的值类似于“╝╝╝╝". 你知道为什么吗?在创建我的其他方法时发生过几次,但我不记得我是如何解决的。再次感谢你。这看起来像是一个编码问题。如果你在一个字符编码中获得字符串的字节,然后用不同的编码重新创建字符串,字符可能会改变。另一方面另一方面,您的编码或解码可能会使数据出错。您可以使用调试器检查RL编码的数组是否符合预期,以及解码的数组是否与输入数组相同。