How to split arraylist String whenever space occurs in java?

I have this input in ArrayList<String> :

 cat eats mouse mouse eats cheese cheese is tasty 

(blank lines should be ignored, as I will read this input from a file)

and I want to convert it to a 2-dimensional array of String, which will have dimensions [no. of elements in ArrayList][3] [no. of elements in ArrayList][3] .

Not. 3 is fixed, that is, each sentence will have 3 words. eg:

 "cat" "eats" "mouse" "mouse" "eats" "cheese" "cheese" "is" "tasty" 

here is what i tried:

  public static int processData(ArrayList<String> array) { String str[]=new String[array.size()]; array.toArray(str); String str1[][]=new String[str.length][5]; for(int i=0;i<str.length;i++) { str1[i][]=str.split("\\s+"); //i want to do something like this, but this is showing errors. } return 0; //this is temporary, I will be modifying it } 

Tell me if I do not understand.

+5
source share
4 answers

You are close. In Java, you cannot put new elements at the end of an array using empty brackets [] . The following code does the thing. Note that the number of elements in the second array is limited to 5. So, after the first 5 words, the rest of the line will be ignored. If the string is shorter, it will be null at the end of the array.

 public static int processData(ArrayList<String> array) { String[] str = new String[array.size()]; array.toArray(str); String[][] str1 = new String[str.length][3]; for(int i=0; i < str.length; i++) { String[] parts = str[i].split("\\s+"); for(int j = 0; j < parts.length || j < 3; j++) { str1[i][j] = parts[j]; } } // do something next } 
+3
source

Shorter and slightly more efficient version:

 static int processData(ArrayList<String> array) { String str[][] = new String[array.size()][3]; for(int i = 0; i < str.length; ++i) { str[i] = array.get(i).split("\\s+"); } return 0; } 

There is no reasion in your code for the first array named str , since you can access strings directly from an ArrayList.

Also you do not need to copy strings, you can just put arrays of strings into an array of arrays, as in my code

Plus, if you have a fixed size of 3 and you no longer need to add arrays, why do you allocate space for 5 lines?

+2
source

Change the for loop to:

 String str1[][] = new String[str.length][3]; for(int i = 0; i < str.length; i++) { str1[i] = str[i].split("\\s+"); } 

You do not need to have 5 elements, if you know that you have only 3 words, do not waste your resources.

str is String[] , str[i] is String , str[i].split() is String[] , which means str1[i] . Types match.

In addition, in this way, the code becomes more understandable and understandable. I agree with Ongy to remove str if you don't need it, but now I can’t say because you said you were going to change this method later (at least the return value)

Bonus : Btw, the names array , str , str1 not the best choice for this part of the code, it is very easy to confuse what is. Try to find a better name like lines , linesArray , words or something like this

0
source

As you mentioned "arraylist" in the subject:

 try{ BufferedReader br = new BufferedReader(new FileReader("filename")); Arraylist<String[]> l = new ArrayList<String[]>(); String line; while((line = br.readline) != null) l.add(line.split("\\s+"); br.close(); }catch(Exception e){e.printStackTrace();} 
0
source

Source: https://habr.com/ru/post/1201961/


All Articles