Is this good coding in java?:
I have an String array. String[] strArr = ...
. Now I want to process on it and get the result, so I made a method taking a String array as an argument like,
public bopolen processArray(String[] srtArr){
// .... some processing
// loop over string array, process it and create a
// list of objects with same size as array
List<Object> objList = new ArrayList<Object>(strArr.length);
for(String str : strArr) {
String[] anotherStrArr = str.split(",");
Object myObj = new MyObject(anotherStrArr[0],anotherStrArr[1]);
objList.add(myObj);
}
// .... some more processing
// ....
// loop over list and call another method
// that also takes an String array as argument like
for (Object obj: objList) { <-- will loop same times as for the String array.
boolean res = processData(obj.getDataMethod(), strArr); <-- again pass the same array as argument to another method.
// This method will get called as many time as array length.
}
}
Now the second method:
public boolean processData(String data, String[] strArr) {
// ..... some processing.
// loop over String array and compare with data to process.
for(String str: strArr) {
String[] againStrArr = str.split(",");
if(againStrArr[0].equals(data)) {
// ... process on data and get the result.
}
}
// ..... other statements of method.
}
So as you can see I'm passing same array to 2 methods and loop over it again and again. Is this good practice in java as my array size is very long, around 2000 elements in normal case.
Explanation on why I'm looping this much:
I got a String array from request which contains db_name and db_score of myObject which I have to update in database. Each element in strArr is comma separated db values like db_name,db_score
. So first I loop over array and create a String of all names comma separated and then query in database to create List of MyObject. Then I loop over this list to update myObj with new score but to get exact score of name I again have to loop over array and compare name then get its score.
This is my table called Players:
id | name | score
1 | mark | 5
2 | mark_1 | 5
3 | mark_2 | 5
4 | mark_3 | 5
Sample data in string array: {"mark,10","mark_1,15","mark_2,20","mark_3,30"}
Now I iterate through array and create comma separated name string and use that in query like:
select * from myObject where name in ('mark','mark_2','mark_2','mark_3')
|_________________________________|
|
this much part is built from looping over the String array
So first iteration is for creating where clause of query. Now this query returns List of MyObject. Second time I iterate through this list to update score of players. But to get score of particular player name I again have to iterate over strArray and find it. So for each element in list I have to call a method and pass playerName and strArr to get it's score and then finally update it in database.