在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.
}
}
现在第二种方法:
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.
解释为什么我这样说:
我从请求中抽出一个阵列,其中载有我必须更新的数据库中的分数。 插图中的每个要素 Arr is comma separate db Value such as db_name,db_s分
。 因此,我先处理阵列,并创建一套所有与 com相分离的名字,然后在数据库中查询,以编制我的物体清单。 然后,我就这份名单提出新记分,以更新我的办公室,但为了获得准确的记分,我再次不得不超越阵列,比较名字,然后取分。
这是我称为“运动员”的表:
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"}
现在,我通过阵列敲响,制造 com子,以示和使用,如:
select * from myObject where name in ( mark , mark_2 , mark_2 , mark_3 )
|_________________________________|
|
this much part is built from looping over the String array
因此,首先就是为了在什么地方提出询问条款。 我现在要问一下MyObject的返回名单。 我第二次通过这份名单来更新数十名参与者。 但是,为了获得特定参与者的分数,我再次不得不绕过大阵,发现它。 因此,就清单中的每一部分而言,我必须使用一种方法,并通过角色Name和方体。 无法取得分数,然后最后更新数据库。