如何获取多行字符串并按空格分割它们然后添加到 Java 中的 ArrayList 中?
输入的第一行是一个整数,表示应该扫描多少行字符串。该输入将用于检查字谜词,然后打印反字谜词。单词应仅用空格分隔,并且应删除破折号 (-)。
示例输入:
4
sali est p-o-s-t try tset luf
set boo ins pick too let sim
set post sho kim lack
flu est test soo tick
public static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
int l = scan.nextInt();
scan.nextLine();
String[][] arr = new String[][];
List<String> ss1 = new ArrayList<String>();
for (int i=0; i<l; i++) {
arr[i] = scan.nextLine().replaceAll("\\-", "").split("\\ |\\n");
String[] dd = arr[i];
String ddd = dd[i];
ss1.add(ddd);
}
}
...
}
或者这个:
public static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
int line = scan.nextInt();
scan.nextLine();
String str = "";
ArrayList<String> shd = new ArrayList<String>();
for (int i=0; i<line; i++) {
str += scan.nextLine();
shd.add(str);
}
}
此输入将用于检查字谜词,然后打印反字谜词。单词应仅用空格分隔,并且应删除破折号 (-)。
它应该用空格分割,如 ( [ sali, est, p-o-s-t, ...]
实际输出:
[sali est p-o-s-t try tset luf,
sali est p-o-s-t try tset lufset boo ins pick too let sim,
sali est p-o-s-t try tset lufset boo ins pick too let simset post sho kim lack,
sali est p-o-s-t try tset lufset boo ins pick too let simset post sho kim lackflu est test soo tick]
最佳答案
你是说?
Scanner io = new Scanner(System.in);
int n = io.nextInt();
io.nextLine();
List<String> list = new ArrayList<>();
for (int i = 1; i <= n; i++) {
String str = io.nextLine();
String join = String.join("", list) + str;
list.add(join);
}
关于java - 如何获取并按空格分割输入的多个字符串行,然后将它们添加到Java中的arrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58690421/