如何编写测试来打印此结果?
package leetcode_one_twenty;
import java.util.HashMap; // HashMap package
public class Two_Sum {
public int[] twoSum(int[] numbers, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < numbers.length; i++) {
if (map.get(numbers[i]) != null) {
int[] result = {map.get(numbers[i]) + 1, i + 1};
return result;
}
map.put(target - numbers[i], i);
}
int[] result = {};
return result;
}
public static void main(String[] args) {
// How can I write a test to print this result? THX!
}
}
答案 0 :(得分:1)
制作twoSum
方法static
,并使用main
方法中的值调用它:
int[] myArray = {1,2,3};
int target = 5;
System.out.println(Arrays.toString(twoSum(myArray, target)));
答案 1 :(得分:0)
我不确定你到底想要做什么。为什么不试试System.out.println()
和toString()
?