Java 如何通过组合两个不同的变量来创建变量名

Java 如何通过组合两个不同的变量来创建变量名,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我从CSV文件中获取值,然后将其存储在字符串中,现在这些字符串是我在Selenium中使用sendKeys传递的数据。现在的问题是,我想将这些字符串的变量与for循环计数结合起来,那么我该怎么做呢 String csvFile = "/home/Miscellaneous/demo.csv"; CSVReader csv_reader = new CSVReader(new FileReader(csvFile)); List<String[]> strings = csv_read

我从CSV文件中获取值,然后将其存储在字符串中,现在这些字符串是我在Selenium中使用sendKeys传递的数据。现在的问题是,我想将这些字符串的变量与for循环计数结合起来,那么我该怎么做呢

String csvFile = "/home/Miscellaneous/demo.csv";
CSVReader csv_reader = new CSVReader(new FileReader(csvFile));
List<String[]> strings = csv_reader.readAll();
for(int dime=2 ;dime<strings.size() ;dime++)
{
//Get data from csv file
    String[] csvCell = strings.get(dime);
    String no_of_new_rows_temp = csvCell[2];
    Integer no_of_new_rows = Integer.valueOf(no_of_new_rows_temp);
    String dim_label_1 = csvCell[3];

    for (int no_of_new_rows_fill = 0; no_of_new_rows_fill <= no_of_new_rows; no_of_new_rows_fill++)
    {
//Add Dimentions
        driver.findElement(By.name("dimension[label]["+no_of_new_rows_fill+"]")).sendKeys("dim_label_" + no_of_new_rows_fill);
    }
}

通过它,我可以将dim_label_1的CSV文件数据发送到字段。

要在java中创建动态变量是不可能的。但是您可以在这里利用java集合来解决这个问题

 String csvFile = "/home/Miscellaneous/demo.csv";
 CSVReader csv_reader = new CSVReader(new FileReader(csvFile));
 List<String[]> strings = csv_reader.readAll();
 Map<String,String> map = new HashMap()<String,String>;
 for(int dime=2 ;dime<strings.size() ;dime++)
 {
 //Get data from csv file
     String[] csvCell = strings.get(dime);
     String no_of_new_rows_temp = csvCell[2];
     Integer no_of_new_rows = Integer.valueOf(no_of_new_rows_temp);
     //String dim_label_1 = csvCell[3];
    map.put("dim_label"+dime,csvCell[3]);
     for (int no_of_new_rows_fill = 0; no_of_new_rows_fill <= no_of_new_rows; no_of_new_rows_fill++)
     {
 //Add Dimentions
         //driver.findElement(By.name("dimension[label]["+no_of_new_rows_fill+"]")).sendKeys("dim_label_" + no_of_new_rows_fill);
        driver.findElement(By.name("dimension[label]["+no_of_new_rows_fill+"]")).sendKeys(map.get("dim_label_" + no_of_new_rows_fill));
     }
 }
String csvFile=“/home/missianeous/demo.csv”;
CSVReader csv_reader=new CSVReader(new FileReader(csvFile));
List strings=csv_reader.readAll();
Map Map=newhashmap();

对于(int dime=2;低于溶液的dimetry
 String csvFile = "/home/Miscellaneous/demo.csv";
 CSVReader csv_reader = new CSVReader(new FileReader(csvFile));
 List<String[]> strings = csv_reader.readAll();
 Map<String,String> map = new HashMap()<String,String>;
 for(int dime=2 ;dime<strings.size() ;dime++)
 {
 //Get data from csv file
     String[] csvCell = strings.get(dime);
     String no_of_new_rows_temp = csvCell[2];
     Integer no_of_new_rows = Integer.valueOf(no_of_new_rows_temp);
     //String dim_label_1 = csvCell[3];
    map.put("dim_label"+dime,csvCell[3]);
     for (int no_of_new_rows_fill = 0; no_of_new_rows_fill <= no_of_new_rows; no_of_new_rows_fill++)
     {
 //Add Dimentions
         //driver.findElement(By.name("dimension[label]["+no_of_new_rows_fill+"]")).sendKeys("dim_label_" + no_of_new_rows_fill);
        driver.findElement(By.name("dimension[label]["+no_of_new_rows_fill+"]")).sendKeys(map.get("dim_label_" + no_of_new_rows_fill));
     }
 }