[英]split each element of array
I have a problem.我有个问题。
I have the following input string:我有以下输入字符串:
String text = "John.Davidson/05082004/Belgrade Michael.Barton/01011998/Krakov Ivan.Perkinson/23051986/Moscow";
I have developed the below code:我开发了以下代码:
public Person(String warehouse) {
// Separating people
String[] peopleArray = warehouse.split(" ");
System.out.println(peopleArray[0]); //John.Davidson/05082004/Belgrade
System.out.println(peopleArray[1]); //Michael.Barton/01011998/Krakov
System.out.println(peopleArray[2]); //Ivan.Perkinson/23051986/Moscow
String[] person1 = peopleArray[0].split("/|\\.");
System.out.println(person1[0]); //John
}
I want to split each element of peopleArray
with the <.split("/|\\.") >
and put every person in a new array我想用
<.split("/|\\.") >
拆分peopleArray
的每个元素,并将每个人放在一个新数组中
The exact request: In the main class, after creating the Person object and filling in its fields, the Person object must be put in the collection (for example, in the list) and at the end a passage must be made through this collection and the data about people must be written.具体要求:在主 class 中,创建 Person object 并填写其字段后,必须将 Person object 放入集合中(例如,在列表中),最后必须通过此集合进行一段操作,必须写关于人的数据。
I think it should look like this:我认为它应该是这样的:
John, Davidson, 05082004, Belgrad
Michael, Barton, 01011998, Krakov,
Ivan, Perkinson, 23051986, Moscow
I think he needs arrays to manipulate them in another moment and not only strings representation of the array.我认为他需要 arrays 来在另一时刻操纵它们,而不仅仅是数组的字符串表示。 Well, you can build a List of arrays and then get the array you want with data from there:
那么,您可以构建一个 arrays 的列表,然后从那里获取您想要的数组和数据:
public static void main(String []args){
// Separating people
String warehouse = "John.Davidson/05082004/Belgrade Michael.Barton/01011998/Krakov Ivan.Perkinson/23051986/Moscow";
List<String[]> nameList = new ArrayList<>();
String[] peopleArray = warehouse.split(" ");
for (String s : peopleArray){
String[] person1 = s.split("/|\\.");
nameList.add(person1);
}
nameList.forEach(x -> {
System.out.println(x[0]);
}); //print only names of ALL the people
System.out.println("\ALL info about the first person and so on... :");
String[] person = nameList.get(0);
for (String s: person){
System.out.println(s);
}
}
Output Output
John
Michael
Ivan
All info about the first person and so on... :
John
Davidson
05082004
Belgrade
Use this instead:改用这个:
String[] person1 = peopleArray[0].split("[\\./]");
You may try use some regex tester eg https://regex101.com/ .您可以尝试使用一些正则表达式测试器,例如https://regex101.com/ 。 But remember to escape some chars eg backslash to double backslash
但请记住转义一些字符,例如反斜杠转为双反斜杠
Another thing is: The peopleArray[0], peopleArray[1], peopleArray[2] actually can be solved using loop eg for loop.另一件事是:peopleArray[0]、peopleArray[1]、peopleArray[2] 实际上可以使用循环解决,例如 for 循环。
You can loop through the peopleArray
and keep the data for each person in a 2D array您可以遍历
peopleArray
并将每个人的数据保存在二维数组中
String[] peopleArray = text.split(" ");
String[][] peopleDetailsArray = new String[peopleArray.length][4];
for (int i = 0; i < peopleArray.length; i++) {
peopleDetailsArray[i] = peopleArray[i].split("/|\\.");
}
System.out.println(Arrays.deepToString(peopleDetailsArray));
Output: Output:
[[John, Davidson, 05082004, Belgrade], [Michael, Barton, 01011998, Krakov], [Ivan, Perkinson, 23051986, Moscow]]
Try this code:试试这个代码:
public class Main {
public static void main(String[] args) {
String text = "John.Davidson/05082004/Belgrade Michael.Barton/01011998/Krakov Ivan.Perkinson/23051986/Moscow";
String[] peopleArray = text.split(" ");
String[][] personArray = new String[peopleArray.length][4];
for(int i=0; i<peopleArray.length; i++){
personArray[i] = peopleArray[i].split("/|\\.");
for(int j=0; j<personArray[i].length; j++) {
System.out.print(personArray[i][j] + " ");
}
System.out.println(" ");
}
}
}
Output: Output:
John Davidson 05082004 Belgrade
Michael Barton 01011998 Krakov
Ivan Perkinson 23051986 Moscow
You can try Java 8 map to get 2D array of Person as -您可以尝试 Java 8 map 来获取 Person 的二维数组 -
String text = "John.Davidson/05082004/Belgrade Michael.Barton/01011998/Krakov Ivan.Perkinson/23051986/Moscow";
String[][] strings = Arrays.stream(text.split(" ")).
map(str -> str.split("/|\\.")).toArray(String[][]::new);
System.out.println(Arrays.deepToString(strings));
Output - Output -
[[John, Davidson, 05082004, Belgrade], [Michael, Barton, 01011998, Krakov], [Ivan, Perkinson, 23051986, Moscow]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.