Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.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 - Fatal编程技术网

Java 替换“;至于;循环以提高性能

Java 替换“;至于;循环以提高性能,java,Java,我需要帮助将url替换为“{data}”的“8687686758768678676867768678”,但不使用for循环,因为将有超过10000个url进入,它将失去性能,因此我希望替换for循环。请在这方面帮助我,因为我在进行此转换时面临问题 private static Boolean moreThen10NumbersInString (ArrayList<String> arrayList) { // this method checks if the url is hav

我需要帮助将url替换为“{data}”的“8687686758768678676867768678”,但不使用for循环,因为将有超过10000个url进入,它将失去性能,因此我希望替换for循环。请在这方面帮助我,因为我在进行此转换时面临问题

private static Boolean moreThen10NumbersInString (ArrayList<String> arrayList) { // this method checks if the url is having more than 8 digits then it replaces it with "{data}"
    int numberOfNumbers = 0;
    String replace = "{Data}";

        for(String string : arrayList){

            for(int j=0; j < string.length(); j++){

                if (Character.isDigit(string.charAt(j))) {
                //System.out.println(string);
                numberOfNumbers++;


                if (numberOfNumbers > 8) { // 8 is used as some standard number to check its numeric content

                    int x = arrayList.indexOf(string);
                    arrayList.remove(x);
                    arrayList.add(x, replace);
                    return true;
                }

            }
            }
        }


    return false;
}

public static void main(String[] args) {

    String url = "/this/is/a/url/with/number/868768675876867867876867768678/replace/to/data"; //this url is just a mock data
    String[] split = url.split("/"); //url will be splitted by "/"
    ArrayList<String> arrayList = new ArrayList<String>();

    for(String s : split){
        //System.out.println(s);
        if(s!=null && s!=""){
            arrayList.add(s);
        }
        else{
            arrayList.remove(s);
        }
    }

    moreThen10NumbersInString(arrayList);

        if(Boolean.TRUE){ //if the url contains numbers part
            for(String str : arrayList){

                System.out.print(str+"/");

            }
        }
        if(Boolean.FALSE) //if the url doesnt contain any numbers part
        {
        System.out.println(url);    
        }

}
private static Boolean morethen10numberinstall(ArrayList ArrayList){//此方法检查url是否超过8位,然后将其替换为“{data}”
int numberOfNumbers=0;
字符串replace=“{Data}”;
for(字符串:arrayList){
对于(int j=0;j8){//8被用作某个标准数字来检查其数字内容
int x=arrayList.indexOf(字符串);
arrayList.remove(x);
arrayList.add(x,replace);
返回true;
}
}
}
}
返回false;
}
公共静态void main(字符串[]args){
字符串url=“/this/is/a/url/with/number/868768675876867867876867768678/replace/to/data”;//此url只是一个模拟数据
String[]split=url。split(“/”;//url将被“/”分割
ArrayList ArrayList=新的ArrayList();
用于(字符串s:拆分){
//系统输出打印项次;
如果(s!=null&&s!=“”){
arrayList.add(s);
}
否则{
arrayList.remove(s);
}
}
超过10个数字的安装(arrayList);
if(Boolean.TRUE){//如果url包含数字部分
for(字符串str:arrayList){
系统输出打印(str+“/”);
}
}
if(Boolean.FALSE)//如果url不包含任何数字部分
{
System.out.println(url);
}
}

}

您要做的就是替换URL中的纯数字路径。为此,只需使用
String.replace

下面使用正则表达式匹配和替换数值路径。以下是您可能需要的所有代码(仅在
main
中):

String url=“/this/is/a/url/with/number/868768675876867867876867768678/replace/to/data”;
//查找“/”并替换为/{data}/
字符串结果=url.replaceAll(“/\\d{8,}/”,“/{data}/”;
系统输出打印项次(结果);

打印
“/this/is/a/url/with/number/{Data}/replace/to/Data/”

您要做的就是替换url中的一个纯数字路径。为此,只需使用
String.replace

下面使用正则表达式匹配和替换数值路径。以下是您可能需要的所有代码(仅在
main
中):

String url=“/this/is/a/url/with/number/868768675876867867876867768678/replace/to/data”;
//查找“/”并替换为/{data}/
字符串结果=url.replaceAll(“/\\d{8,}/”,“/{data}/”;
系统输出打印项次(结果);

打印
“/this/is/a/url/with/number/{Data}/replace/to/Data/”

问题不在于使用for循环。您正在不必要地从列表中添加/搜索/删除。您实际上想做什么?问题不是您正在使用for循环。您正在不必要地添加/搜索/从列表中删除。您实际上想做什么?
url.replace
不支持正则表达式,请使用
url.replaceAll
而不是根据OP的代码判断,他需要替换8+个数字,因此regexp将是
/\\d{8,}/
instead@BackSlash我被太多的输出搞糊涂了。回到理智上来,你是对的,我可以解释。谢谢。
url.replace
不支持正则表达式,请使用
url.replaceAll
代替。根据OP的代码判断,他需要替换8+个数字,因此regexp将是
/\\d{8,}/
instead@BackSlash我被太多的输出搞糊涂了。回到理智上来,你是对的,我可以解释。谢谢
String url = "/this/is/a/url/with/number/868768675876867867876867768678/replace/to/data";

//finds "/<numbers>/" and replaces with /{data}/
String result = url.replaceAll("/\\d{8,}/", "/{data}/");
System.out.println(result);