英文:
split each element of array
问题
public Person(String warehouse) {
// Separating people
String[] peopleArray = warehouse.split(" ");
for (String personData : peopleArray) {
String[] personInfo = personData.split("/|\\.");
// Ensure there are enough elements in personInfo before accessing them
if (personInfo.length >= 4) {
String firstName = personInfo[0];
String lastName = personInfo[1];
String dateOfBirth = personInfo[2];
String city = personInfo[3];
// Now, you can do whatever you want with this person's data
// For example, you can create a Person object and add it to a collection
// or simply print the data
System.out.println(firstName + ", " + lastName + ", " + dateOfBirth + ", " + city);
}
}
}
在主类中,创建了Person对象并填充了其字段之后,可以将Person对象放入集合中(例如,列表),然后通过该集合进行遍历,并将有关人员的数据写入。
英文:
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
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.
I think it should look like this:
John, Davidson, 05082004, Belgrad
Michael, Barton, 01011998, Krakov,
Ivan, Perkinson, 23051986, Moscow
答案1
得分: 1
我认为他需要数组以便在另一个时刻对它们进行操作,而不仅仅是数组的字符串表示。好吧,你可以构建一个数组列表,然后使用那里的数据获取你想要的数组:
public static void main(String[] args) {
// 分离人员信息
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]);
}); // 仅打印所有人的名字
System.out.println("所有第一个人的信息以及接下来的信息...:");
String[] person = nameList.get(0);
for (String s : person) {
System.out.println(s);
}
}
输出结果
John
Michael
Ivan
所有第一个人的信息以及接下来的信息...:
John
Davidson
05082004
Belgrade
英文:
I think he needs arrays to manipulate them in another moment and not only strings representation of the array. Well, you can build a List of arrays and then get the array you want with data from there:
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
John
Michael
Ivan
All info about the first person and so on... :
John
Davidson
05082004
Belgrade
答案2
得分: 0
请使用以下代码替代:
String[] person1 = peopleArray[0].split("[\\./]");
你可以尝试使用正则表达式测试工具,例如 https://regex101.com/。但请记住要转义某些字符,例如反斜杠转为双反斜杠。
另一件事是:
peopleArray[0]、peopleArray[1] 和 peopleArray[2] 可以使用循环解决,例如 for 循环。
英文:
Use this instead:
String[] person1 = peopleArray[0].split("[\\./]");
You may try use some regex tester e.g. https://regex101.com/. But remember to escape some chars e.g. backslash to double backslash
Another thing is:
The peopleArray[0], peopleArray[1], peopleArray[2] actually can be solved using loop e.g. for loop.
答案3
得分: 0
你可以循环遍历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));
输出:
[[John, Davidson, 05082004, Belgrade], [Michael, Barton, 01011998, Krakov], [Ivan, Perkinson, 23051986, Moscow]]
英文:
You can loop through the peopleArray
and keep the data for each person in a 2D array
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:
[[John, Davidson, 05082004, Belgrade], [Michael, Barton, 01011998, Krakov], [Ivan, Perkinson, 23051986, Moscow]]
答案4
得分: 0
尝试这段代码:
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(" ");
}
}
}
输出:
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 :
John Davidson 05082004 Belgrade
Michael Barton 01011998 Krakov
Ivan Perkinson 23051986 Moscow
答案5
得分: 0
你可以尝试使用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));
输出结果为:
[[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 -
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 -
[[John, Davidson, 05082004, Belgrade], [Michael, Barton, 01011998, Krakov], [Ivan, Perkinson, 23051986, Moscow]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论