如何获取多行字符串并将其按空格分割,然后添加到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]