【问题标题】:Synchronized arrayList- incompatible types同步的arrayList-不兼容的类型
【发布时间】:2013-08-30 04:31:31
【问题描述】:

我想在 JAVA 中创建同步的 ArrayList。我用过这段代码:

static ArrayList<User> listOfUsers = Collections.synchronizedList(new ArrayList<User>());

但是 NetBeans 大喊:

不兼容的类型:

必需:数组列表 找到:列表

有什么帮助吗?

【问题讨论】:

    标签: java arraylist synchronized


    【解决方案1】:

    List&lt;User&gt; listOfUsers = Collections.synchronizedList(new ArrayList&lt;User&gt;());

    为我工作

    【讨论】:

      【解决方案2】:

      Collections.synchronizedList 返回一个新的SynchronizedList&lt;User&gt; 实例,并转换回List&lt;User&gt;。如果您的声明成功

      static List&lt;User&gt; listOfUsers = Collections.synchronizedList(new ArrayList&lt;User&gt;());

      ,会好的。

      【讨论】:

        【解决方案3】:
        static List<User> listOfUsers = Collections.synchronizedList(new ArrayList<User>());
        

        应该解决问题。

        【讨论】:

          【解决方案4】:

          Collections.synchronizedList() 返回 List,而不是 ArrayList。像这样声明用户列表:

          static List<User> listOfUsers = Collections.synchronizedList(new ArrayList<User>());
          

          【讨论】:

          • 不是说“List”类型不带任何参数。
          • @user2721537 当然可以。除了java.util.List,您还导入了其他List 吗?
          • 确实:我有 java.awt.List Thx ;-)
          【解决方案5】:
          Collections.synchronizedList()
          

          返回List。使用类似的东西:

          static List<User> listOfUsers = Collections.synchronizedList(new ArrayList<User>());
          

          每个 ArrayList 都是 List 但每个 List不是 ArrayList .

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-09-07
            • 2012-10-19
            • 2013-10-29
            • 2018-03-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-07-17
            相关资源
            最近更新 更多