[英]Java - Two Dimensional array - only value of last row of array is getting assigned correctly rest are assigned with null
I have an XML as below format; 我有以下格式的XML;
<TestCase>
<Step Sel = "deleteAllVisibleCookies" Obj = "All cookies" Val = ""></Step>
<Step Sel = "open" Obj = "URL" Val = "UserName:Password"></Step>
<Step Sel = "waitForElementPresent" Obj = "link=mobile" Val = ""></Step>
<Step Sel = "clickAndWait" Obj = "Mobile link" Val = ""></Step>
...
</TestCase>
<TestCase>
<Step Sel = "deleteAllVisibleCookies" Obj = "All cookies" Val = ""></Step>
<Step Sel = "open" Obj = "URL" Val = "UserName:Password"></Step>
<Step Sel = "waitForElementPresent" Obj = "link=mobile" Val = ""></Step>
<Step Sel = "clickAndWait" Obj = "Mobile link" Val = ""></Step>
...
</TestCase>
based on above XML file, I am creating an object. 基于以上XML文件,我正在创建一个对象。 I am trying to save all steps in to two dimensional array.
我试图将所有步骤保存到二维数组中。 So one row is one test case.
因此,一行是一个测试用例。
int i=0;
int j=0;
for (int TC = 0; TC < TCLst.getLength(); TC++)
int k=0;
Node TCLstNode = TCLst.item(TC);
if (TCLstNode.getNodeType() == Node.ELEMENT_NODE)
{
NodeList StepLst = TCLstNode.getChildNodes();
Step = new String [TCCount][StepLst.getLength()];//defining total length
Sel = new String [TCCount][StepLst.getLength()];
Obj = new String [TCCount][StepLst.getLength()];
Val = new String [TCCount][StepLst.getLength()];
for (int Step = 0; Step < StepLst.getLength(); Step++)
{
Node StepLstNode = StepLst.item(Step);
if (StepLstNode.getNodeType() == Node.ELEMENT_NODE)
{
if (StepLstNode.getNodeName() == "Step")
{
Sel[i][k] = ObjectType.getAttribute(StepLstNode,"Sel");//returns value of Sel attribute
Obj[i][k] = ObjectType.getAttribute(StepLstNode,"Obj");
Val[i][k] = ObjectType.getAttribute(StepLstNode,"Val");
stepCountInTC++;
k++;
}
}//NodeType
}//for
i++;
stepCountInATCOfModule[j] = stepCountInTC;
j++;
stepCountInTC = 1;
}//TC if
Issue which I am facing is after the object is created, on printing anyone of the two dimensional arrays i am getting output as (here i used Sel attribute); 我面临的问题是在创建对象之后,在打印二维数组中的任何一个时,我将输出为(这里我使用了Sel属性);
[[null, null, null, null,...][deleteAllVisibleCookies, open, waitForElementPresent, clickAndWait,...]] [[null,null,null,null,...] [deleteAllVisibleCookies,打开,waitForElementPresent,clickAndWait,...]
Problem here first test case value is saved as null. 这里的问题是第一个测试用例的值被保存为空。 If i use XML with 3 test cases then first 2 test cases are saving as null and third case is properly saved to array.
如果我将XML与3个测试用例一起使用,则前2个测试用例将保存为null,第三个案例将被正确保存到数组。
Also please suggest any collection to use instead of two dimensional array. 还请建议使用任何集合,而不要使用二维数组。
You miss the braces here: 您在这里错过了大括号:
for (int TC = 0; TC < TCLst.getLength(); TC++)
int k=0;
The code after this 2 rows are independent of this for
because java thinks you do this: 这2行后的代码是独立的这一
for
因为Java认为你这样做:
for (int TC = 0; TC < TCLst.getLength(); TC++) {
int k=0;
}
thus k
will always be 0
. 因此
k
始终为0
。
There are multiple problems with you code which need fixing. 您的代码有多个问题,需要解决。 For example:
例如:
Step = new String [TCCount][StepLst.getLength()];
and 和
int Step = 0
Step
is eiher an int
or something else (which is not visible in the code) mixing them up is not a good thing. Step
是将int
或其他某种东西(在代码中不可见)混合起来不是一件好事。 You also no use camelCase
variable names. 您也无需使用
camelCase
变量名。
In every outer loop you override the whole array with a new one here: 在每个外部循环中,您可以在此处使用新数组覆盖整个数组:
Step = new String [TCCount][StepLst.getLength()];//defining total length
Means at the end you find only the last result on the Step variable. 意思是最后您只能在Step变量上找到最后一个结果。
You have to do something like this: 您必须执行以下操作:
int length = 10;
String[][] Step = new String[length][];
for (int i = 0; i < length; i++)
{
int innerLength = 2;
Step[i] = new String[innerLength];
for (int j = 0; j < innerLength; j++)
{
Step[i][j] = "Value";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.