Java中的压缩和解压缩 [英] Compression and Decompression in Java

查看:138
本文介绍了Java中的压缩和解压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难想出一种解压缩java中的字符串的方法,这是一个基本的java类,所以它只需要基本的命令,没什么太花哨的。它的目标是能够输入

Im having trouble coming up with a way too decompress a string in java, this is a basic java class im taking so it only requires basic commands, nothing too fancy. The objective it to able to type

C:\>java Compress -c aaaabbbbbcc 



in命令提示符,它将打印 a4b5c2 (就像压缩参数字符串一样)。



另一个目标是键入


in the command prompt and it will print a4b5c2 (like it is compress the argument string).

The other objective is to type

C:\>java Compress -d a5b7c4



它将打印 aaaaabbbbbbbcccc (就像它将解压缩参数字符串一样)。减压是我的问题。这是我的代码,我得到的任何帮助都非常感谢。




and it will print aaaaabbbbbbbcccc (like it will decompress the argument string). The decompression is the issue Im having. Here is my code, any help i get is much appreciated.

import java.util.*;
public class Compress{
	public static void main(String args[]){

		Scanner scan=new Scanner(System.in);
		String originalString = scan.nextLine();
		int number = scan.nextInt();
		
		if(args[0].equals("-c"))
		{
			System.out.println("compress");
            compress(originalString);
		}
	
		else if(args[0].equals("-d"))
		{
			System.out.println("decompress");
            decompress(originalString);
		}
		else{System.out.println("Compress program by Kelsey Faram");
		System.out.println("usage: java Compress [-c,-d] < inputFile > outputFile");
		}
	}
	
	
	public static void compress(String originalString)
	{
		int count = 0;
		char comp = originalString.charAt(0);
		for(int i=0; i < originalString.length(); i++){
		originalString.charAt(i);
		
		if(comp==originalString.charAt(i))
		count++;
		else{
			System.out.print(comp+""+count);
			comp=originalString.charAt(i); count=1;
		}	
		} System.out.println(comp+""+count);
	}
	
	public static void decompress(String originalString)
    {
		
	}
	
	
}

推荐答案

我不会给你代码,但会尽力引导你朝着正确的方向前进。



这似乎是一个简单的RLE类型压缩,因此解压缩非常简单。

一旦你做了,你需要了解压缩方法的作用这个,解压缩应该是显而易见的。



压缩将扫描输入,并为每个字符输出字符,然后计数。所以解压缩应该很简单



1.从字符串中取一个字符(x),

2.取下一个字符并转换为一个int(y)

3.输出字符(x),(y)次数



将字符转换为int在这种情况下非常简单,使用基本ASCII,字符0的ASCII值为48,1值的值为49等,所以:



int count = originalString.charAt(i) - 48;





也不是说这只适用于你的重复次数超过9个字符的情况串。改变压缩函数以考虑到这一点可能是有用的,这样如果你有一个像aaaaaaaaaaaaa那样的序列,那么outpput将是a9a5。
I will not give you the code, but will try to guide you in the right direction.

This seems to be a simple RLE type compression, so decompression is very simple.
You need to understand what the compress method does, once you do this, the decompress should be obvious.

The compress will scan the input and for each character it finds output the character, followed by a count. So decompressing should be simple

1. take a character (x) from the string,
2. take the next character and convert to an int (y)
3. Output the character (x), (y) number of times

Converting a character to an int in this case is really simple, using basic ASCII, the character 0 has an ASCII value of 48, 1 has a value of 49 etc, so :

int count = originalString.charAt(i) - 48;


Also not that this only works if you never have more than 9 character repetitions in your string. It might be usefull to change the compress function to take that into account so that if you have a sequence like aaaaaaaaaaaaaa, the outpput will be a9a5.


这篇关于Java中的压缩和解压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆