Ho un ingresso come questo in un ArrayList<String>
:come dividere un arraylist di String ogni volta che si incontra uno spazio in java?
cat eats mouse
mouse eats cheese
cheese is tasty
(righe vuote devono essere ignorate da quando mi sarà la lettura di questo input da un file)
e voglio convertirlo in un 2- d array di String che avrà dimensioni [no. of elements in ArrayList][3]
.
Il n. 3 è fisso, cioè ogni frase avrà 3 parole. come questo:
"cat" "eats" "mouse"
"mouse" "eats" "cheese"
"cheese" "is" "tasty"
ecco cosa ho provato:
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
}
Ditemi se non mi è chiaro.
Non so perché ma str.split() sta mostrando gli errori: 'Impossibile richiamare split (String) sul tipo di matrice String []' –
Oh, capisco. È perché str è una matrice. Lo aggiusterò .. –
Inoltre, una pratica migliore è dichiarare gli array come 'String [] str', non' String str [] '. –