由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - modify parameter, or return?
相关主题
List, LinkedList and VectorBufferedWriter里的write()
immutable list请教:怎样函数里改变一个Double变量的值?
functional programming why? (转载)请问有没有generic的array
how do I get parameters passed by text area (form能否让函数返回一个用于赋值的引用
A design for parameter passingJavascript speed up
synchronized method does lock the object that passed into the method as a parameter?想说动同事用java而不是c++做新项目,大家能否列举一下优点?
invoke a function dynamically in Java?java EL 问题请教
一个Java程序员的话(3)java 的函数 xxx(a, b,c)能够向a 写入数据吗?
相关话题的讨论汇总
话题: objecta话题: list话题: alist
进入Java版参与讨论
1 (共1页)
b******y
发帖数: 1684
1
if you have:
aMethod {
...
List aList;
...
}
You need to do some kind of conversion of aList inside aMethod.
Would you do it
void convert1(List aList, conversionParameter1,
conversionParameter2) {
foreach (ObjectA a : aList) {
a.convert(conversionParameter1, conversionParameter2);
}
}
or
List convert2(List aList, conversionParameter1,
conversionParameter2) {
List aConvertedList = new ArrayList();
foreach (ObjectA a : aList) {
a.convert(conver
u****s
发帖数: 2186
2
the 2nd one.

【在 b******y 的大作中提到】
: if you have:
: aMethod {
: ...
: List aList;
: ...
: }
: You need to do some kind of conversion of aList inside aMethod.
: Would you do it
: void convert1(List aList, conversionParameter1,
: conversionParameter2) {

h*****0
发帖数: 4889
3
这个取决于需求吧。第一个方法是转换。第二个方法是转换加新copy了一份。
我觉得一般来说第一个比较合适。因为可以第一个外加一个copy来实现第二个。

【在 b******y 的大作中提到】
: if you have:
: aMethod {
: ...
: List aList;
: ...
: }
: You need to do some kind of conversion of aList inside aMethod.
: Would you do it
: void convert1(List aList, conversionParameter1,
: conversionParameter2) {

c*****t
发帖数: 1879
4
Depends on if listA has to be immutable.

【在 h*****0 的大作中提到】
: 这个取决于需求吧。第一个方法是转换。第二个方法是转换加新copy了一份。
: 我觉得一般来说第一个比较合适。因为可以第一个外加一个copy来实现第二个。

h*****0
发帖数: 4889
5
楼主的代码好像没有这个区别。List本身都没有变,而List里面的元素都变了。

【在 c*****t 的大作中提到】
: Depends on if listA has to be immutable.
u****s
发帖数: 2186
6
List 本身在method call的时候是不能变的,元素可以变
第一种是典型的C++ way,
第二中是典型的Java way

【在 h*****0 的大作中提到】
: 楼主的代码好像没有这个区别。List本身都没有变,而List里面的元素都变了。
s*****i
发帖数: 355
7
支持第二种.

【在 u****s 的大作中提到】
: List 本身在method call的时候是不能变的,元素可以变
: 第一种是典型的C++ way,
: 第二中是典型的Java way

h*****0
发帖数: 4889
8
为什么不能变?
h*****0
发帖数: 4889
9
你们真的都读了那段代码?还是我哪里看错了?

【在 s*****i 的大作中提到】
: 支持第二种.
h*****0
发帖数: 4889
10
事实上这里的逻辑是在convert List里的每一个元素,而不是convert List本身。不明
白为啥要新建一个。

【在 b******y 的大作中提到】
: if you have:
: aMethod {
: ...
: List aList;
: ...
: }
: You need to do some kind of conversion of aList inside aMethod.
: Would you do it
: void convert1(List aList, conversionParameter1,
: conversionParameter2) {

相关主题
synchronized method does lock the object that passed into the method as a parameter?BufferedWriter里的write()
invoke a function dynamically in Java?请教:怎样函数里改变一个Double变量的值?
一个Java程序员的话(3)请问有没有generic的array
进入Java版参与讨论
h*****0
发帖数: 4889
11
另外,foreach关键字是什么时候加入java的?我圡,还真没用过……

【在 b******y 的大作中提到】
: if you have:
: aMethod {
: ...
: List aList;
: ...
: }
: You need to do some kind of conversion of aList inside aMethod.
: Would you do it
: void convert1(List aList, conversionParameter1,
: conversionParameter2) {

s*****i
发帖数: 355
12
我以前有过一个类似的例子,这个list多线程只读不写,然后定期更新这个list。我觉
得第二种好

【在 h*****0 的大作中提到】
: 你们真的都读了那段代码?还是我哪里看错了?
h*****0
发帖数: 4889
13
……
我再说一遍,第一个方法也同样“没有写原来的list”!!

【在 s*****i 的大作中提到】
: 我以前有过一个类似的例子,这个list多线程只读不写,然后定期更新这个list。我觉
: 得第二种好

s*****i
发帖数: 355
14
我知道。个人偏好,我喜欢所有东西转换好了以后,再一次把aList换了。而不是一个
一个元素在里面转换。因为同时可能有其它线程在读

【在 h*****0 的大作中提到】
: ……
: 我再说一遍,第一个方法也同样“没有写原来的list”!!

h*****0
发帖数: 4889
15
第一个方法:一个一个转换。
第二个方法:一个一个转换,转换的同时丢进一个新的List里。
对第一个方法的任何线程不安全,对第二个方法同样不安全。

【在 s*****i 的大作中提到】
: 我知道。个人偏好,我喜欢所有东西转换好了以后,再一次把aList换了。而不是一个
: 一个元素在里面转换。因为同时可能有其它线程在读

s******n
发帖数: 876
16
no difference in this case.

【在 b******y 的大作中提到】
: if you have:
: aMethod {
: ...
: List aList;
: ...
: }
: You need to do some kind of conversion of aList inside aMethod.
: Would you do it
: void convert1(List aList, conversionParameter1,
: conversionParameter2) {

b******y
发帖数: 1684
17
how about
List convert3(List aList, conversionParameter1,
conversionParameter2) {
List aConvertedList = new ArrayList();
foreach (ObjectA a : aList) {
ObjectA converted = a.convertNew(conversionParameter1, conversionParameter2);
aConvertedList.add(converted);
}
return aConvertedList;
}

【在 h*****0 的大作中提到】
: 事实上这里的逻辑是在convert List里的每一个元素,而不是convert List本身。不明
: 白为啥要新建一个。

g*****g
发帖数: 34805
18
I would worry about thread-safe only
when thread-safe is necessary.

【在 h*****0 的大作中提到】
: 第一个方法:一个一个转换。
: 第二个方法:一个一个转换,转换的同时丢进一个新的List里。
: 对第一个方法的任何线程不安全,对第二个方法同样不安全。

h*****0
发帖数: 4889
19
。。。
convertNew是干嘛?返回this?

conversionParameter2);

【在 b******y 的大作中提到】
: how about
: List convert3(List aList, conversionParameter1,
: conversionParameter2) {
: List aConvertedList = new ArrayList();
: foreach (ObjectA a : aList) {
: ObjectA converted = a.convertNew(conversionParameter1, conversionParameter2);
: aConvertedList.add(converted);
: }
: return aConvertedList;
: }

h*****0
发帖数: 4889
20
我知道为什么你们一堆人都说方法2好了。
因为你们心里想的都不是ObjectA,你们想的是int
对于一个List,在convert的时候用的是:
for (int a : list) {
int b = convert(a);
anotherList.add(b);
}
这个当然有道理,特别是考虑线程安全时。
然而在楼主的方法里,原来就是ObjectA而不是int型的东西,用的是:
a.convert();
而不是
b = a.convert();
所以逻辑完全不同了。

【在 b******y 的大作中提到】
: if you have:
: aMethod {
: ...
: List aList;
: ...
: }
: You need to do some kind of conversion of aList inside aMethod.
: Would you do it
: void convert1(List aList, conversionParameter1,
: conversionParameter2) {

相关主题
能否让函数返回一个用于赋值的引用java EL 问题请教
Javascript speed upjava 的函数 xxx(a, b,c)能够向a 写入数据吗?
想说动同事用java而不是c++做新项目,大家能否列举一下优点?NullPointerException 问题
进入Java版参与讨论
h*****0
发帖数: 4889
21
除非convertNew的效果是不改变a,但是返回一个新建的对象。否则依然没有意义。

【在 h*****0 的大作中提到】
: 。。。
: convertNew是干嘛?返回this?
:
: conversionParameter2);

b******y
发帖数: 1684
22
返回一个新建的对象啦

【在 h*****0 的大作中提到】
: 除非convertNew的效果是不改变a,但是返回一个新建的对象。否则依然没有意义。
m******t
发帖数: 2416
23

So I would use convert1 typically , convert3 iff there is a contract
explicitly mandates the list staying unchanged (but then in that case
I would have encapsulated the list in a class).
convert2 doesn't make any sense.
If thread-safty is an issue, I would explicitly synch on the list
rather than convert3, because if there is another thread modifying
the list at the same time, the for loop might still run into problems.

【在 b******y 的大作中提到】
: 返回一个新建的对象啦
s******e
发帖数: 493
24
Another thing you should understand is that java method calls are always "by
value". This is the exact reason that you can never write "swap" function
to swap two strings like you nomarlly do in C++.
s*****i
发帖数: 355
25
如果convert整个list需要很长时间,第二种方法好。先在一个新建的list里面把原来
的值copy过来,然后一个个convert。最后只需要锁一次,一次性赋值就行了。比一个
一个在原list里面转换,整个for loop都锁住list要好点吧?

【在 m******t 的大作中提到】
:
: So I would use convert1 typically , convert3 iff there is a contract
: explicitly mandates the list staying unchanged (but then in that case
: I would have encapsulated the list in a class).
: convert2 doesn't make any sense.
: If thread-safty is an issue, I would explicitly synch on the list
: rather than convert3, because if there is another thread modifying
: the list at the same time, the for loop might still run into problems.

g*****g
发帖数: 34805
26
If performance is an issue, you should look into concurrent package.
ConcurrentSkipListSet or CopyOnWriteArrayList depends on the need.

【在 s*****i 的大作中提到】
: 如果convert整个list需要很长时间,第二种方法好。先在一个新建的list里面把原来
: 的值copy过来,然后一个个convert。最后只需要锁一次,一次性赋值就行了。比一个
: 一个在原list里面转换,整个for loop都锁住list要好点吧?

s*****i
发帖数: 355
27
是,或者用reentrant read/write lock

【在 g*****g 的大作中提到】
: If performance is an issue, you should look into concurrent package.
: ConcurrentSkipListSet or CopyOnWriteArrayList depends on the need.

b******y
发帖数: 1684
28
if parameter1 and parameter2 are both List type,
it would be a headache...
my concern on convert1 like methods is that you don't know which
parameter is in&out (mutable), and which one is immutable.
while with convert3, you can follow a convention that every parameter
is immutable

【在 m******t 的大作中提到】
:
: So I would use convert1 typically , convert3 iff there is a contract
: explicitly mandates the list staying unchanged (but then in that case
: I would have encapsulated the list in a class).
: convert2 doesn't make any sense.
: If thread-safty is an issue, I would explicitly synch on the list
: rather than convert3, because if there is another thread modifying
: the list at the same time, the for loop might still run into problems.

m******t
发帖数: 2416
29

I agree. It comes with a price however. That's why I would use it
only when it's necessary. For instance, when you are exposing this
as a public api which guarantees no side-effects.
OTOH, if this is just some implementation code and you have full
control over both sides of the call boundary, then it'd be an overkill
to actively enforce this.

【在 b******y 的大作中提到】
: if parameter1 and parameter2 are both List type,
: it would be a headache...
: my concern on convert1 like methods is that you don't know which
: parameter is in&out (mutable), and which one is immutable.
: while with convert3, you can follow a convention that every parameter
: is immutable

b******y
发帖数: 1684
30
正好要修一个bug
结果看code一层层这样modify parameter的,改起来太麻烦了。
i.e., 不得不多看很多source code

【在 m******t 的大作中提到】
:
: I agree. It comes with a price however. That's why I would use it
: only when it's necessary. For instance, when you are exposing this
: as a public api which guarantees no side-effects.
: OTOH, if this is just some implementation code and you have full
: control over both sides of the call boundary, then it'd be an overkill
: to actively enforce this.

1 (共1页)
进入Java版参与讨论
相关主题
java 的函数 xxx(a, b,c)能够向a 写入数据吗?A design for parameter passing
NullPointerException 问题synchronized method does lock the object that passed into the method as a parameter?
一个简单的关于java Map的问题invoke a function dynamically in Java?
问个primitive type的问题一个Java程序员的话(3)
List, LinkedList and VectorBufferedWriter里的write()
immutable list请教:怎样函数里改变一个Double变量的值?
functional programming why? (转载)请问有没有generic的array
how do I get parameters passed by text area (form能否让函数返回一个用于赋值的引用
相关话题的讨论汇总
话题: objecta话题: list话题: alist