使用 split() 方法时利用字符串的不同部分 JAVA

Utilizing the Different Parts of a String when Using the split() Method JAVA

我正在使用这个 while 循环遍历名为 s 的字符串数组列表。

int i = 0;
while (i < s.length()) {

  i++;
  s.get(i).split(",");

}

我正在尝试 split() s 的每一行使用分隔符 ","。 我想将每一行的每一部分放入一个新的 Product 对象中,如下所示: new Product(s.get(i) first part, s.get(i) second part)

我找不到一种方法来捕获和利用我正在拆分的字符串的每个部分。

String[] result = s.get(i).split(",");

result 包含字符串的各个拆分部分。

并且在您的 while 循环中将长度方法从 s.length()

更正为 s.length

split方法returns一个字符串数组。

此外,使用 for 循环:

for (int i=0; i<s.length(); i++) {
    String[] parts = s.get(i).split(",");
    Product product = new Product(parts[0], parts[1]);
}