File tree Expand file tree Collapse file tree 1 file changed +71
-0
lines changed
Java/Data-Structures/Array Expand file tree Collapse file tree 1 file changed +71
-0
lines changed Original file line number Diff line number Diff line change 1+ //Program to decode RLE data
2+
3+ public class Main
4+ {
5+ public static void main (String [] args ) {
6+
7+ //the data that has to be decoded
8+ byte data [] = {3 ,15 ,6 ,4 };
9+
10+ //the decoded data
11+ //calling method decodeRLE()
12+ byte decoded [] = decodeRLE (data );
13+
14+ //print the decoded data
15+ for (int i =0 ;i <decoded .length ;i ++){
16+ System .out .println (decoded [i ]);
17+ }
18+
19+ }
20+
21+ //method decodedRLE
22+ public static byte [] decodeRLE (byte [] rleData ){
23+
24+ //size of the data that has to be decoded
25+ int n = rleData .length ;
26+
27+ //array to store the numbers which represent the repeating data
28+ byte repeats [] = new byte [n /2 ];
29+
30+ //array to store the numbers that have to be repeated
31+ byte data2 [] = new byte [n /2 ];
32+
33+ //size of the new decoded array
34+ int size = 0 ;
35+
36+ int j = 0 ;
37+
38+ //find the repeats
39+ for (int i =0 ;i <n ;i +=2 ){
40+ repeats [j ] = rleData [i ];
41+ size += repeats [j ];
42+ j ++;
43+ }
44+
45+ j =0 ;
46+
47+ //find the numbers that have to be repeated
48+ for (int i =1 ;i <n ;i +=2 ){
49+ data2 [j ] = rleData [i ];
50+ j ++;
51+ }
52+
53+ //create new array to store the decoded data
54+ byte decode [] = new byte [size ];
55+
56+ int l =0 ;
57+
58+ //decode the data
59+ //use two for loops
60+ for (int i =0 ;i <n /2 ;i ++){
61+ for (byte k =0 ;k <repeats [i ];k ++){
62+ decode [l ] = data2 [i ];
63+ l ++;
64+ }
65+ }
66+
67+ //return the decode array
68+ return decode ;
69+ }
70+
71+ }
You can’t perform that action at this time.
0 commit comments