2015-04-29 121 views
-2

在下面的代码中;Java - 字符串值未保存在字符串数组中

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

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

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

请建议为什么字符串没有被保存在二维数组中。

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

(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 
} 
+6

理论上,这应该起作用。你可以给出这种情况,或者至少可以用嵌套循环运行代码吗? – rickcnagy

+1

不是一个明确的代码段来说什么 –

+1

请张贴完整的相关代码。 – Bhoot

回答

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 

一切都应该按预期工作。

+0

谢谢阿布舍克,它正在工作。 –