使用多个字符串分配For循环值-java

使用多个字符串分配For循环值-java,java,for-loop,Java,For Loop,下面是For循环 //Declare 5 String variable String p1, p2, p3, p4, p5; for (int row = 1; row <= 5; row++) { String pno = driver.findElement( By.xpath("//*[@id='body']/table/tbody/tr[" + row + "]/td")).getText(); p1 = pno; } 我的问题 执行第一行ro

下面是For循环

//Declare 5 String variable
String p1, p2, p3, p4, p5;

for (int row = 1; row <= 5; row++) {
    String pno = driver.findElement(
        By.xpath("//*[@id='body']/table/tbody/tr[" + row + "]/td")).getText();
    p1 = pno;
}
我的问题

执行第一行row=1时,应在p1变量中分配该值

执行第二行row=2时,应在p2变量中分配该值 反之亦然

在java中,如何用单独的变量分配每行值呢?您正在寻找数组或ArrayList

或者如果您不确定有多少个元素

arrayList.add(someNewValue);
如果要动态调整集合大小,可以使用ArrayList。i、 当你不知道你需要的确切尺寸时

ArrayList<String> p = new ArrayList<String>();
for (int row = 1; row <= 5; row++) {
    String pno = driver.findElement(
                    By.xpath("//*[@id='body']/table/tbody/tr[" + row + "]/td")).getText();
    p.add(pno);
}

希望您现在理解

您需要count++对吗?给我们的答案一些反馈。
//Declare 5 String variable
String p1, p2, p3, p4, p5;

String[] pArray = new String[5]; // create an array to store values

for (int row = 1; row <= 5; row++) {
    String pno = driver.findElement(
        By.xpath("//*[@id='body']/table/tbody/tr[" + row + "]/td")).getText();
    pArray[row - 1] = pno; // store value to an array
}

// put the values in the array to the Strings originally created
p1 = pArray[0];
p2 = pArray[1];
p3 = pArray[2];
p4 = pArray[3];
p5 = pArray[4];
String[] p = new String[5]; // length is 5
for (int row = 1,count=0; row <= 5; row++,count++) {
    String pno = driver.findElement(
                 By.xpath("//*[@id='body']/table/tbody/tr[" + row + "]/td")).getText();
    p[count] = pno;
}
//Declare 5 String variable
String p1, p2, p3, p4, p5;

String[] pArray = new String[5]; // create an array to store values

for (int row = 1; row <= 5; row++) {
    String pno = driver.findElement(
        By.xpath("//*[@id='body']/table/tbody/tr[" + row + "]/td")).getText();
    pArray[row - 1] = pno; // store value to an array
}

// put the values in the array to the Strings originally created
p1 = pArray[0];
p2 = pArray[1];
p3 = pArray[2];
p4 = pArray[3];
p5 = pArray[4];