我能找到的最接近的是这个帖子:
答案很清楚,你定义了一个int [] tempArray并使用tempArray作为参数.但是,我的问题是为什么我不能直接将int数组写为参数.
代码示例如下:
public static void main (String[] args) {
NewClass test = new NewClass();
// int[] tempArray = {1, 2, 3};
// test.doSomething(tempArray);
test.doSomething({1, 2, 3});
}
Run Code Online (Sandbox Code Playgroud) 我只想做一些初始检查,并在必要时快速返回 {-1, -1} 向量。不知怎的,编译器说:我应该将返回类型更改为向量*
但在执行此预检查代码之前,当前的返回类型适用于我的后续部分。那么我误解了什么?
class SomeClass {
public:
static vector<int> solution(vector<int>& numbers, int target) {
if (numbers.empty() || numbers.size() < 2) {
return new vector<int> {-1, -1}; // <== Compile Error
}
unordered_map<int, int> hash;
vector<int> result;
.
.
.
return result;
}
};
int main() {
vector<int> testNums = {11, 15, 2, 7};
vector<int> result = SomeClass::solution(testNums, 9);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 非常简单的python,我只是通过复制旧列表来创建一个新的列表对象.通过使用.copy()方法,我认为应该创建一个基于官方文档Python中的新对象:https://docs.python.org/3.6/library/stdtypes.html?highlight=list#list
后为什么我更新了新对象中的元素,旧列表对象中的元素也发生了变化.
old = [[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]
print(old)
new = old.copy()
for i in range(len(new)):
for j in range(len(new[0])):
new[i][j] = 0
print(old)
print(new)
Run Code Online (Sandbox Code Playgroud)
为什么输出是,我期望旧值不应该改变:
[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
Run Code Online (Sandbox Code Playgroud)