If you want to know the conversion method quickly, please see the conclusion at the end of the page.
Continuing from the last time, it is a coding test material again. [Java 8] Sorting method in alphabetical order and string length order that can be used in coding test --Qiita
Obviously, in coding tests, most of the flow is "acquisition of value by standard input"-> "conversion for easy processing"-> "some kind of calculation processing". I don't want to spend time on "acquiring values by standard input" and "converting for my own processing" in order to spend even a little time on "some kind of calculation processing".
This time, as the title suggests, I will summarize how to convert standard input to a list or array.
A common standard input in question is an image like the one below.
1,AIUEO,Kakikukeko
Suppose that the problem is to get characters separated by commas (,) and output the characters after them by the number entered first. In the above example, the first number is 1, so the output is
AIUEO
Is the correct answer.
2,AIUEO,Kakikukeko,SA Shi Su Se So
If is a standard input
AIUEO
Kakikukeko
Is the correct answer.
In such cases, I would like to read the string first and store it in a comma separated list.
First, get the standard input as follows.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
There seems to be a method of acquisition by Scanner, but I am using a pattern that uses java.io.BufferedReader and java.io.InputStreamReader. The character string for one line is stored in line.
Next, let's try the process of storing in the List separated by commas (,) in the main subject.
List<String> list = Arrays.asList(line.split(","));
It's easy, but that's it.
split splits a string at a position that matches the specified regular expression. The return value of this is String []. It is a string type array. Since it is split by a regular expression, it can be split by anything other than a comma (,). The next step is to use the java.util.Arrays.asList method to convert the array to a list.
It may be independent as a method as shown below.
/**
*Convert from String to List
* @param str Converted String
* @return Converted List
*/
private static List<String> stringToList(String line) {
return Arrays.asList(line.split(","));
}
If you answer the problem that you have thought about, the source code will be as follows. ~~ It's okay to use an array instead of a List! !! And You don't need to convert it to List! !! Please do not say "Tsukomi" ......... ~~
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
String line = br.readLine();
List<String> list = Arrays.asList(line.split(","));
int cnt = Integer.parseInt(list.get(0));
for (int i = 1; i <= cnt; i++) {
System.out.println(list.get(i));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
If it is a List,
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
List<String> list = Arrays.asList(line.split(","));
If it is an array
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
String[] strArray = line.split(",");
Recommended Posts