Reputation: 4191
I believe there are many similar questions, sorry if this is too common. I want to learn which one is better/faster/space efficient etc and why.
public static void(String[] main){
//case 1
String[] str_arr = new String[n];
method1(str_arr)
//case 2
String[] str_arr = new String[n];
String[] arr = new String[n];
for(int i=0; i < n; i++){
arr[i] = str_arr[i].split("some_char")[2];
}
method2(arr);
}
void method1(String[] str_arr){
String[] arr = new String[n];
for(int i=0; i < n; i++){
arr[i] = str_arr[i].split("aChar")[2];//assume there are 50 of aChar
}
// do_something with arr ;
}
void method2(String[] arr){
// do_something with arr ;
}
Which one should I prefer?
Thanks in advance.
Upvotes: 0
Views: 72
Reputation: 769
This is purely up to your discretion.
When it comes to performance reasons:
In terms of clean and tidy code:
TL;DR Go for the first option, and eventually make some modification.
Also, it may be a good idea to split the elements before actually putting them inside the array.
Upvotes: 1