0

I'm a python programmer, but currently I'm reading through Java code to get some ideas. I have no programming experience at all with Java and I don't know how it's possible, but I couldn't get any information using Google about these functions.

if(pv.size() -2 < j)
     pv.add(j+1, localpv.get(j));
else
     pv.set(j+1, localpv.get(j));

This is the piece of code I need to decypher. pv and localpv are both vectors (I believe they are equivalent to lists in python?), and something is added to them. I can guess that one of them is adding them to a vector at a certain position (j+1), but then I have no idea what the other one does.

Can you please explain those two lines for me and maybe telling what are they equivalent to in python?

4

4 回答 4

3

add 在指定位置插入指定元素

set 替换指定位置的元素

于 2012-10-07T19:22:17.010 回答
2

签出 JavaDocs http://docs.oracle.com/javase/6/docs/api/java/util/Vector.html

add 在将所有其他对象向后移动的位置插入一个对象。set 覆盖该位置的当前对象。

于 2012-10-07T19:21:35.670 回答
2

您可以在API 参考中查找所有Java 方法的定义。

Vector.add(int index, E element)

在此 Vector 中的指定位置插入指定元素。

Vector.set(int index, E element)

将此 Vector 中指定位置的元素替换为指定元素。

等效的 Python 代码是

if len(pv) - 2 < j:
     pv.insert(j+1, localpv[j])
else:
     pv[j+1] = localpv[j]
于 2012-10-07T19:23:54.890 回答
0

第一个在 j+1'st 位置添加一个新元素,另一个将现有 j+1'st 位置的值设置为给定值。

我猜作者想确保他不会尝试设置列表(向量)中不存在的元素的值。

于 2012-10-07T19:20:30.130 回答