替换没有循环的索引

时间:2016-01-28 06:30:24

标签: java

我的问题是:

首先我有1000个字符的String变量,我有另一组1000字符的String变量
1]在第一组变量中包含" 1110000XXXXX0001111 ...."像这样等等,直到1000

2]在第二组变量中包含" 1110000101010001111 ..."像这样等等到1000年

3]我需要在第一个变量中获取X的位置并从第二个变量中替换相似位置的值

例如:第一个数据变量" 000XXX000X0"          第二个数据变量" 00011000010" X应该被第二组数据中位置的值替换。

注意:在没有循环的情况下完成 因为如果我们把循环的循环放在循环中1000次并且' X'字符串

中可以是1000个字符的任何位置

例如:1记录1000次
如果100K记录表示1000 * 100K(性能故障)

因此需要解决方案。

请帮我解决这个问题。

我的代码是:

 String    sInputStr="0X11XXXXX000000000000000000000000000000000000000000000000X000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011";

 String sDbStr="0111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011";


          int iLength=sInputStr.length();
            for(int i=0;i<iLength;i++){
                if(sInputStr.charAt(i)=='X'){

                }else{
                    if(i>sDbStr.length()){
                        break;
                    }else{
                        sChar[i] = sInputStr.charAt(i);                         
                    }
                }
            }//End of For
            sVal=String.valueOf(sChar);
            System.out.println("sVal == " +sVal);

帮助我的朋友

4 个答案:

答案 0 :(得分:1)

你需要的就是这样的东西

public static boolean isGameOver(int score, int lives, int level) {

    return ((level == 1 && score < 1001)
         || (level == 2 && score < 2001)
         || (level == 3 && score < 3001));
}

样本结果:

    class FirstApp {
    public static void main(String[] args) {
        String sDbStr="0111111110000001234000000000000011";
        StringBuilder sNewStr= new StringBuilder("011111111000000XXXX00000000000001112");
        String findStr = "X";
        int lastIndex = 0;
        System.out.println("Starting");
        long startTime = System.currentTimeMillis();
        String result = replaceValues("X", sDbStr, sNewStr);
        long endTime = System.currentTimeMillis();
        System.out.println("Result");
        System.out.println(result);
        System.out.println(String.valueOf(endTime-startTime));
    }

    public static String replaceValues(String toReplace, String fromStr, StringBuilder toStr) {
        int lastIndex = toStr.indexOf(toReplace);
        if(lastIndex != -1){
            toStr.replace(lastIndex,lastIndex+1,Character.toString(fromStr.charAt(lastIndex)));
            System.out.println(toStr);
            return replaceValues(toReplace, fromStr, toStr);
        } else {
            return toStr.toString();
        }
    }
}

更新使用stringBuilder和递归更新解决方案以确保更少的执行时间

答案 1 :(得分:0)

如果X指向一个值,就像提到X一样,请替换为1。转到string.replaceAll功能。

        String oriString="000XXX000X0";
        String replaceOne=oriString.replace('X','1');
        System.out.println(replaceOne);

答案 2 :(得分:0)

如果我正确理解了问题,那么您希望将第一个数组中的X值替换为相同位置的秒数组中的值。例如,array1: 000XXX000&amp; array2: 100101001。然后array1最终应该是000101000

这是一个简单的代码片段来实现这个目标:

char[] arr1 = sInputStr.toCharArray();
char[] arr2 = sDbStr.toCharArray();

for(int i = 0; i < arr1.size(); i++)
     if(arr1[i] == 'X')
          arr1[i] == arr2[i];

答案 3 :(得分:0)

这个想法是搜索字符出现的索引说“X&#39;只要我们找到&#39; X&#39;并将第二个字符串中的所有字符复制到第一个字符串中。重复该过程直到最后一次出现&#39; X&#39;。

$destinationPath = publicpath().'imagens/perfil';