【问题标题】:API java List and Collection [closed]API java列表和集合[关闭]
【发布时间】:2014-08-27 11:10:45
【问题描述】:

这是什么意思 它是一个数组或者它是一个列表类型的字符串

List<String> list = new ArrayList<String>(); // why parenthesis
List<String> removeList = new ArrayList<String>();//why parenthesis

还有这个方法 那么List和Collection和arrys有什么区别

// what mean this collection
    private void removeColors(Collection<String> collection1,Collection<String> collection2)
    {
        Iterator<String> iterator = collection1.iterator();//what does mean
        while(iterator.hasNext())//what does mean
            if(collection2.contains(iterator.next()))//what does mean
                iterator.remove();//what does mean
    }

【问题讨论】:

  • 它是为了编译时类型安全,所以你不会在你的 List 中添加其他东西,这些东西只有 String
  • 括号是必需的,因为你正在调用一个方法(构造函数)
  • 在提出问题之前,您应该阅读一些基本教程。请参阅 Java 中的泛型
  • arraylist 和 list 的区别见本页:stackoverflow.com/questions/8028892/…

标签: java list api collections arraylist


【解决方案1】:

【讨论】:

    【解决方案2】:

    Collection 是 - 数据集合。这是任何类型的集合(列表、地图、树...)的基本接口。任何集合都是(或应该是)Iterable,这意味着您可以遍历集合的所有元素(例如,在循环中)。

    List 显然也是一个集合,因为列表是数据的集合。这就是 List 接口扩展 Collection 接口的原因。

    但是,由于实现 List 的方法有很多,因此 List 只是一个接口而不是一个类。 ArrayList 通过包装一个数组来实现一个列表。另一个例子是LinkedList,它通过将元素相互链接来存储数据。我不想讨论它们的优缺点(你可以查一下)。

    最后要考虑的是,您存储在集合(列表)中的数据具有类型。通常,您只会在特定集合中存储一种类型的 Object,这就是 Collection 接口(及其所有子接口,如 List)采用类型参数的原因。此参数指定您希望存储在列表中的数据类型,这有利于类型安全和方便。

    现在,在您的代码中:

    List<String> list = new ArrayList<String>();
    List<String> removeList = new ArrayList<String>();
    

    您创建了一个名为“list”的变量。这个变量的类型是List,意思是:一个字符串列表。使用 new 运算符,您可以创建实际对象。显然,您必须选择一个实现,并且您选择了“ArrayList”。自然,您希望集合中包含字符串,因此您将字符串指定为类型参数。由于您正在调用 ArrayList 的构造函数,因此您需要空括号(因此您调用不带任何参数的构造函数)。 第二行代码也是如此。

    //this method takes two collections
    //that means that you can pass any type of collection (including list) to it
    //the advantage is that you could also pass another type of collection if you chose to do so
    private void removeColors(Collection<String> collection1,Collection<String> collection2)
    {
        Iterator<String> iterator = collection1.iterator();//this line takes the iterator of your first collection
        //an iterator is an object that allows you to go through a collection (list)
        //you can get the objects one by one by calling the next() method
        while(iterator.hasNext())//basically, that's what you're doing here:
        //you let the loop continue as long as there are more items inside the iterator, that is, the first collection
            if(collection2.contains(iterator.next()))//so if there's another item, you take it by calling next() and check if the second collection contains it
                iterator.remove();//if that's the case, you remove the item from the first collection
    }
    //what you've basically achieved:
    //you removed all the items from the first collection that you can find in the second collection as well, so you could say:
    //collection1 = collection1 - collection2
    

    现在,您可以用数据(字符串)填充您在上面创建的字符串列表,并对 removeList 执行相同操作,然后通过调用从列表中“减去”removeList:

    removeColors(list, removeList);
    

    【讨论】:

      【解决方案3】:

      在 Java 中,数组具有无法更改的特定大小。如果您来自其他脚本语言,例如 php,这对您来说可能是新的。

      这就是为什么经常使用例如ArrayLists 的原因。你有一个动态大小,你可以根据需要添加和删除元素。

      List&lt;String&gt; 告诉编译器只接受字符串 (https://en.wikipedia.org/wiki/Type_safety)。

      List 是 Collection 的特化。

      类型参数: E - 此列表中元素的类型

      所有超接口: 集合,可迭代

      来源:http://docs.oracle.com/javase/7/docs/api/java/util/List.html

      更多信息:

      http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

      【讨论】:

        【解决方案4】:

        解释如下

        List<String> list = new ArrayList<String>(); // to call constructor for creating Arraylist of type String
        List<String> removeList = new ArrayList<String>(); // same as above
        
        
        private void removeColors(Collection<String> collection1,Collection<String> collection2)
        {
            Iterator<String> iterator = collection1.iterator();//to get typesafe iterator of underlying collection
            while(iterator.hasNext())//to check if there is another element in collection
                if(collection2.contains(iterator.next()))
                              //interator.next() - to get next String from collection
                              //collection2.contains(..) - to check if String already presents in another collection
                iterator.remove();//remove element from iteration and move to next element
        }
        

        【讨论】:

          猜你喜欢
          • 2016-02-12
          • 1970-01-01
          • 2013-10-28
          • 1970-01-01
          • 2017-07-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多