为什么将null添加到数组的第一个位置?

时间:2017-05-17 18:17:34

标签: java

在deCipherVar数组的第一个位置,它应该只是值1,但是在1(IE.null1)之上添加了一个null值。我不明白在哪里生成空值。

package DeCipher;

public class ShiftDecipher
{

private static String deCipher, gatherVar, shiftVar;

/**
 * in order for tempVarHolder to work we need to add a value to the front of
 * the encrypted string so that we can intitilize its length with the shift
 * length, or pass shiftTimes through Constructor
 * */

private static String[] deCipherVar;
private static String[] tempVarHolder;

private static int cipherVarCount = 0;
private static int shiftTimes;

public ShiftDecipher()
{
    deCipher = "";
}

public ShiftDecipher(String toDeCipher, int shiftTime)
{
    deCipher = toDeCipher;
    shiftTimes = shiftTime;
    split();
}

/**
 * Is used to parse out all variables in the string into an array
 * */
public void split()
{
    tempVarHolder = new String[deCipher.length()];
    for (int i = 0; i < deCipher.length(); i++)
    {
        tempVarHolder[i] = deCipher.substring(i, i + 1);
        System.out.println(tempVarHolder[i]);
    }


    getDeCipherVariables();
}

/**
 * Goes through and sorts the variables out into their perspective
 * values(pre-injection)
 * */
public void getDeCipherVariables()
{
    deCipherVar = new String[(shiftTimes * 2)];
    deCipherVar[0] ="1";
    deCipherVar[1]="2";
    System.out.println("Cipher Length: " + deCipherVar.length);
    for(int i = 0; i < deCipherVar.length; i++)
    {
        System.out.println(deCipherVar[i]);
    }
    for (int i = 0; i < tempVarHolder.length; i++)
    {
        if (tempVarHolder[i].matches(".*\\d") && tempVarHolder[i].matches("[1-9]"))
        {
            gatherVar += tempVarHolder[i];
            System.out.println(gatherVar);
        }else if(tempVarHolder[i].matches(".*\\d") && cipherVarCount < deCipherVar.length){
            deCipherVar[cipherVarCount] = gatherVar;
            cipherVarCount++;
            gatherVar = "";
        }
    }

    /**
     * Removing Variables from string
     * */

    deCipher = deCipher.replace("0","");

    shiftDeCipher();
}

/**
 * !*account for first 1's true value being zero*!
 * */
public static String shiftDeCipher()
{

    System.out.println("DeCipher: " + deCipher);

    return "";
}

}

输出

String to encode: 
encode

Number of times to encode: 
1
toEncode length: 6
:>Check(1)
:>Run(2)
2
xgvhwx1
Runing times: 1
xgvh : 10150
xgvh10150wx1
:> Encoded Shift Check: xgvh10150wx1 = 12
x g v h 1 0 1 5 0 w x 1 
Cipher Length: 2
1
2
null1 
1 
15 
1 
DeCipher: xgvh115wx1

1 个答案:

答案 0 :(得分:0)

您永远不会初始化gatherVar,因此它会以null

开头

稍后在您的代码中,您有以下这一行:gatherVar += tempVarHolder[i];

扩展您的gatherVar = gatherVar + tempVarHolder[i];

此时gahterVar为nulltempVarHolder[i]为“1”,表达式变为null + "1"。在字符串连接中使用null会将其转换为字符串"null",因此null + "1"变为"null1"