例如我有这个数组
String[] mailToCc = {"[email protected]; [email protected];", "[email protected]"};
第一个元素是 2 封邮件,末尾有分号。我想要的是将这两个元素分开(它们可以在数组的任何元素上找到,并不总是第一个)并将它们像新元素一样添加到原始数组中。它应该是这样的
["[email protected]", "[email protected]", "[email protected]"];
这是我的尝试
import java.util.ArrayList;
import java.util.Arrays;
public class Main
{
public static void main(String[] args) {
ArrayList<String> updatedMailToCc = new ArrayList<>();
String[] mailToCc = {"[email protected]; [email protected];", "[email protected]"};
for (String element : mailToCc) {
String[] splitElements = element.split(";");
// Add the original element itself
updatedMailToCc.add(element);
// If it could be split, add each part as a new element
if (splitElements.length > 1) {
updatedMailToCc.addAll(Arrays.asList(splitElements));
}
}
// Convert the ArrayList back to an array if needed
mailToCc = updatedMailToCc.toArray(new String[0]);
// Print the updated array
System.out.println("resultado =" + Arrays.toString(mailToCc));
}
}
但我没有得到我需要的东西,而是得到了这个结果
["[email protected]; [email protected];", "[email protected]", "[email protected]", "[email protected]"]