Java - 字符串值未保存在String数组中

时间:2015-04-29 04:49:32

标签: java arrays

在下面的代码中;

stepOrExpected[i][k] = "Step";
System.out.println(stepOrExpected[i][k]);

通过使用带有i和k的循环,我在stepOrExpected(二维数组)中保存值。这里的问题是String没有被保存。如果我在为stepOrExpected指定值后立即打印,则打印为空。

此外,如果我打印i和k,两个值都是正确的。

请建议为什么String不会以二维数组保存。

我已粘贴下面的代码,我面临的问题是

(if (StepLstNode.getNodeName().equals("Step"))
{
stepOrExpected[i][k] = "Step";)

实际代码:

if (ModulenameLstNode.getNodeType() == Node.ELEMENT_NODE) 
{
    NodeList TCLst = ModulenameLstNode.getChildNodes();

    TCCount = docInput.getElementsByTagName("TC").getLength();
    System.out.println(TCCount);
    TCID = new String[TCCount];
    //TCDepend = new String[TCCount];
    TCDescription = new String[TCCount];
    stepCountInATCOfModule = new int[TCCount];
    ExpectedResult = new String[TCCount];
    ActualResult_Pass = new String[TCCount];
    ActualResult_Fail = new String[TCCount];
    //Result.XMLCreator();
    int i=0;
    stepOrExpected = new String [TCCount][100];
    Action = new String [TCCount][100];
    RefObj = new String [TCCount][100];
    Val = new String [TCCount][100];

    for (int TC = 0; TC < TCLst.getLength(); TC++) 
    {

        int k=0;
        Node TCLstNode = TCLst.item(TC);

        if (TCLstNode.getNodeType() == Node.ELEMENT_NODE) 
        {
            System.out.println(TCLstNode.getNodeName());

            TCID[i] = ObjectType.getAttribute(TCLstNode,"Id");
            //TCDepend[i] = ObjectType.getAttribute(TCLstNode,"Depend");
            TCDescription[i] = ObjectType.getAttribute(TCLstNode,"Description");

            NodeList StepLst = TCLstNode.getChildNodes();


            System.out.println(StepLst.getLength());
            for (int Step = 0; Step < StepLst.getLength(); Step++) 
            {
                Node StepLstNode = StepLst.item(Step);

                if (StepLstNode.getNodeType() == Node.ELEMENT_NODE) 
                {

                    if (StepLstNode.getNodeName().equals("Step"))
                    {
                        stepOrExpected[i][k] = "Step";
                        Action[i][k] = ObjectType.getAttribute(StepLstNode,"Action");
                        RefObj[i][k] = ObjectType.getAttribute(StepLstNode,"RefObj");
                        Val[i][k] = ObjectType.getAttribute(StepLstNode,"Val");
                        stepCountInTC++;
                        k++;
                        System.out.println(i);
                        System.out.println(k);
                        System.out.println(ObjectType.getAttribute(StepLstNode,"Action"));
                        System.out.println(stepOrExpected[i][k]);
                    }
                    else if (StepLstNode.getNodeName().equals("Expected"))
                    {
                        stepOrExpected[i][k] = "Expected";
                        Action[i][k] = ObjectType.getAttribute(StepLstNode,"ExpAction");
                        RefObj[i][k] = ObjectType.getAttribute(StepLstNode,"ExpTarget");
                        Val[i][k] = ObjectType.getAttribute(StepLstNode,"ExpVal");
                        stepCountInTC++;
                        k++;
                    }
                    else if (StepLstNode.getNodeName().equals("ExpectedResult"))
                    {
                        ExpectedResult [i] = StepLstNode.getTextContent();
                    }
                    else if (StepLstNode.getNodeName().equals("ActualResult_Pass"))
                    {
                        ActualResult_Pass [i] = StepLstNode.getTextContent();
                    }
                    else
                    {
                        ActualResult_Fail [i] = StepLstNode.getTextContent();
                    }

                }//Step NodeType

            }//Step for


            stepCountInATCOfModule[i] = stepCountInTC;
            i++;
            stepCountInTC = 0;

        }//TC if

    }//TC for
}

1 个答案:

答案 0 :(得分:4)

罪魁祸首是k++;。在将其打印到控制台之前,您正在递增k。因此,请按以下方式修改代码,

stepOrExpected[i][k] = "Step";
Action[i][k] = ObjectType.getAttribute(StepLstNode,"Action");
RefObj[i][k] = ObjectType.getAttribute(StepLstNode,"RefObj");
Val[i][k] = ObjectType.getAttribute(StepLstNode,"Val");   
System.out.println(i);
System.out.println(k);
System.out.println(ObjectType.getAttribute(StepLstNode,"Action"));
System.out.println(stepOrExpected[i][k]);
stepCountInTC++; //moved counter
k++; //moved counter

按照预期,一切都应该有效。