English 中文(简体)
Java coding作风——将“努力”阵列再次采用多种方法
原标题:Java coding style - passing array of String to multiple methods again and again

在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和方体。 无法取得分数,然后最后更新数据库。

问题回答

Don。 当你穿过阵列时,阵列本身并没有提到。 将阵列传送到你喜欢的众多方法中,这是十分值得称道的;阵列的大小对工作产生了任何影响(除了方法本身之外)。

Instead of the objList You can use the array of MyObject the length of this is equal to the length of the strArr I guess you can use only method to process array and data and use only one for loop as the strArr is already available so the count of iteration is defined.

您的最后解释是,你不需要再重复两次,在这里,你无法解决问题:

First solution
Instead of creating the WHERE clause for all items, why not just update directly with the name being the WHERE clause. For example

loop through all names and scores (your original array)
    split the name and score
    execute an update on database(param_player_name,param_player_score)

最新说明:

UPDATE Players  
SET score = param_player_score  
WHERE name = param_player_name;

Second solution (not recommended, but just for info)
Instead of looping again, just add your objects to a HashMap instead of an array. example:

Map<String,Long> myMap = new HashMap<String,Long>();
loop through all names and scores (your original array)
     myMap.put(name,score);

这样,你就能够打上<代码>。 (二) 请您:

Long score = myMap.get(name);

正如我说过的那样,下一个解决办法并不是建议的,因为仍然需要你制定很长的<代码>。 WHERE 条款不是一个好主意,如果参与者人数过多,可能会造成问题。

IMPORTANT NOTE
When dealing with database, always use the ID of the column instead of anything else when updating and retrieving data. So, your WHERE should filter by your id column always, supposing it s Unique and NOT NULL whereas your Name field by not be Unique and might be NULL

Java的运行环境具有良好的记忆管理。 它甚至清理了未使用的变数......因此,你甚至可以宣布一个包含数百万元素的阵列。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签