2016-09-25 69 views
0

我想检查这个函数的结果。 但无法修复声明错误!如何声明hashmap?

import java.util.HashMap; 


    public class twosum { 
     public int[] twoSum(int[] nums, int target) { 
Map<Int, Int> map = new HashMap<>(); 

的java:泛型不支持在-source 1.3 的java:钻石经营者未在-source 1.3

  for(int i=0;i<nums.length;i++){ 
       map.put(nums[i],i); 
      } 
      for(int i=0;i<nums.length;i++){ 
       int complement = target - nums[i]; 
       if(map.containsKey(complement) && map.get(complement) != i){ 
        return new int[] { i, map.get(complement)}; 
       } 
      } 
      throw new IllegalArgumentException("No two sum solution"); 
     } 
     public void main(String[] args){ 

      int nums[] = {1,2,3,4}; 
      System.out.println(twoSum(nums,5)); 

      for(int i=0;i<4;i++){ 
       System.out.println(i+"->"+"map="+map); 
      } 
     } 
    } 
+2

是否使用一个IDE?将项目Java级别设置为1.8。 – Li357

+2

这看起来像您的IDE设置为以Java 1.3模式构建。这是_very_老,你应该改变它,理想的是Java 8.您使用哪个IDE? –

回答

0

支持尝试

Map<Integer, Integer> map = new HashMap<>(); 

Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 
+3

这无助于修复所描述的*“java:generics不支持-source 1.3 java:d​​iamond运算符在-source 1.3中不受支持”的错误*。当然,一旦错误得到纠正,下一个错误将会被你所建议的内容所固定,但是你并没有回答实际的*问题*。我不会投票,因为你的答案*有点帮助,但它不是完全有用*,所以没有投票权。 – Andreas