Java_数组
1、当new 一个对象时,要在堆里开辟一个新的空间
2、
Java返回两个变量的方法:return new int[] { i, j };
LeetCode求两数之和的题:
class Solution {
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] == target - nums[i]) {
return new int[] { i, j };
}
}
}
throw new IllegalArgumentException("No two sum solution");
}
}
作者:LeetCode
链接:https://leetcode-cn.com/problems/two-sum/solution/liang-shu-zhi-he-by-leetcode-2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
3、数组有一个缺点:一旦数组创建了,程序运行期间长度不能改变。想要改变就要使用ArrayList

浙公网安备 33010602011771号