分割数组中的每个元素

huangapple go评论89阅读模式
英文:

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 = &quot;John.Davidson/05082004/Belgrade Michael.Barton/01011998/Krakov Ivan.Perkinson/23051986/Moscow&quot;;

    List&lt;String[]&gt; nameList = new ArrayList&lt;&gt;();
    String[] peopleArray = warehouse.split(&quot; &quot;);

    for (String s : peopleArray){
          String[] person1 = s.split(&quot;/|\\.&quot;); 
          nameList.add(person1);
    }
   
   nameList.forEach(x -&gt; {
       System.out.println(x[0]);
       }); //print only names of ALL the people
       
   System.out.println(&quot;\ALL info about the first person and so on... :&quot;);

    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(&quot;[\\./]&quot;);

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(&quot; &quot;);
String[][] peopleDetailsArray = new String[peopleArray.length][4];
for (int i = 0; i &lt; peopleArray.length; i++) {
  peopleDetailsArray[i] = peopleArray[i].split(&quot;/|\\.&quot;);
}
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 = &quot;John.Davidson/05082004/Belgrade Michael.Barton/01011998/Krakov Ivan.Perkinson/23051986/Moscow&quot;;
        
        String[] peopleArray = text.split(&quot; &quot;);
        String[][] personArray = new String[peopleArray.length][4];
        for(int i=0; i&lt;peopleArray.length; i++){
            personArray[i] = peopleArray[i].split(&quot;/|\\.&quot;);
            for(int j=0; j&lt;personArray[i].length; j++) {
                System.out.print(personArray[i][j] + &quot; &quot;);
            }
            System.out.println(&quot; &quot;);
        }
    }
}

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 = &quot;John.Davidson/05082004/Belgrade Michael.Barton/01011998/Krakov Ivan.Perkinson/23051986/Moscow&quot;;

String[][] strings = Arrays.stream(text.split(&quot; &quot;)).
                     map(str -&gt; str.split(&quot;/|\\.&quot;)).toArray(String[][]::new);

System.out.println(Arrays.deepToString(strings));

Output -

[[John, Davidson, 05082004, Belgrade], [Michael, Barton, 01011998, Krakov], [Ivan, Perkinson, 23051986, Moscow]]

huangapple
  • 本文由 发表于 2020年8月2日 16:11:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63213820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定