如何在Java中将列表列表转换为字符串?

时间:2019-07-18 16:27:52

标签: java string arraylist

我正在尝试读取具有某些URL的.CSV文件,将其解析为ArrayList,然后使用该列表将URL传递给接受String作为参数的方法。

我对如何将列表List<List<String>>的列表转换为字符串感到困惑。

public static void main(String[] args) throws Exception {
//Load .CVS location into File instance 
File myFile = new File("test.csv");
List<List<String>> urls = new ArrayList<>();
urls = readCVS(myFile);
LaunchWebsite(urls.get(x))
}

public static void LaunchWebsite(String url) {
        WebDriver driver = new ChromeDriver();
        try {
            driver.get(url);
            Thread.sleep(20000);
            driver.quit();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

public static List<List<String>> readCSV(File fileName) throws IOException{
        List<List<String>> urlsList = new ArrayList<>();
        String line;
        try(BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            while ( (line = br.readLine()) != null){
                String[] values = line.split(",");
                urlsList.add(Arrays.asList(values));
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return urlsList;
    }

我得到的错误是

Type mismatch: cannot convert from List<String> to String

我知道LaunchWebsite需要一个字符串,但是我该如何转换呢?

1 个答案:

答案 0 :(得分:0)

在阅读@jordans评论后想通了。

String x = urls.get(1).toString();