Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 压缩字符串方法是';不返回新字符串吗?_Java_String_Methods_Compression - Fatal编程技术网

Java 压缩字符串方法是';不返回新字符串吗?

Java 压缩字符串方法是';不返回新字符串吗?,java,string,methods,compression,Java,String,Methods,Compression,我正在尝试制作一个压缩字符串的方法。例如,该方法将采用“TTTTEST”并返回“4te2st” 当我运行该方法并打印结果时,我得到:“ 公共类压缩{ 公共静态字符串压缩(字符串原始){ 整数计数=1; int oglength=original.length()-1; StringBuilder newword=新StringBuilder(“”); for(int i=0;i

我正在尝试制作一个压缩字符串的方法。例如,该方法将采用“TTTTEST”并返回“4te2st”

当我运行该方法并打印结果时,我得到:“

公共类压缩{
公共静态字符串压缩(字符串原始){
整数计数=1;
int oglength=original.length()-1;
StringBuilder newword=新StringBuilder(“”);
for(int i=0;i1){
newword.append(newword);
newword.append(count);
newword.append(original.charAt(i));
打破
}如果(original.charAt(i)!=original.charAt(i+k)和&original.indexOf(original.charAt(i+k))-original.indexOf(original.charAt(i))==1){
newword.append(newword);
newword.append(original.charAt(i));
计数++;
打破
}
}
}
字符串returnword=newword.toString();
返回单词;
}

压缩方法在重复使用增量索引时不附加任何内容

以下是基于您的方法的工作版本:

public static String compress(String original) {
    int count;
    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < original.length(); i = i + count) {
        count = 1;
        for (int k = i + 1; k < original.length(); k++) {
            if (original.charAt(i) == original.charAt(k)) {
                count++;
            } else {
                break;
            }
        }

        if (count > 1) {
            builder.append(count).append(original.charAt(i));
        } else {
            builder.append(original.charAt(i));
        }
    }
    return builder.toString();
}
公共静态字符串压缩(字符串原始){
整数计数;
StringBuilder=新的StringBuilder();
for(int i=0;i1){
builder.append(count).append(original.charAt(i));
}否则{
生成器.append(原件.charAt(i));
}
}
返回builder.toString();
}

我认为你对你的程序感到困惑,因为你有
不必要的循环
,它增加了复杂性,而
if-else梯形图
比较了你无法记住的字符(这就是
调试
可能对你有帮助的地方)

我不明白您想要了解的是什么用例,但若您想要的正是上面提到的内容,那个么下面的代码片段将为您提供帮助

注意:
我只考虑了基本的场景。对于大量的测试用例,可能需要很少的小改动

public class Demo {

public static String compress(String original){
    int count = 0;
    char temp = original.charAt(0);
    StringBuilder sb = new StringBuilder("");
    for(int i=0;i<original.length();i++) {

        if(original.charAt(i)==temp) {
            count++;
        }else {
            if(count!=1)
                sb.append(count).append(temp);
            else
                sb.append(temp);

            count=1;

            temp=original.charAt(i);
            continue;
        }

    }
    if(count!=1)
        sb.append(count).append(temp);
    else
        sb.append(temp);
    //Above four lines get last character 
    //and its occurrence if it's more than 1. 


    return sb.toString();
}

public static void main(String args[]) {
    System.out.println(compress("ttttesst")); 
//Enter desired String value here.
//Also you may make it user-interactive.
}
}
公共类演示{
公共静态字符串压缩(字符串原始){
整数计数=0;
字符温度=原始字符(0);
StringBuilder sb=新的StringBuilder(“”);

对于(int i=0;i是否尝试打印字符值的出现次数?如果是,则输出应为2s5t1e,与您的输入一致。字符串保持相同的顺序,并且只有相同字符的连续字符串具有该字符出现次数的值。
public class Demo {

public static String compress(String original){
    int count = 0;
    char temp = original.charAt(0);
    StringBuilder sb = new StringBuilder("");
    for(int i=0;i<original.length();i++) {

        if(original.charAt(i)==temp) {
            count++;
        }else {
            if(count!=1)
                sb.append(count).append(temp);
            else
                sb.append(temp);

            count=1;

            temp=original.charAt(i);
            continue;
        }

    }
    if(count!=1)
        sb.append(count).append(temp);
    else
        sb.append(temp);
    //Above four lines get last character 
    //and its occurrence if it's more than 1. 


    return sb.toString();
}

public static void main(String args[]) {
    System.out.println(compress("ttttesst")); 
//Enter desired String value here.
//Also you may make it user-interactive.
}
}