API java列表和集合

时间:2014-08-27 11:10:45

标签: java list api collections arraylist

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

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
    }

4 个答案:

答案 0 :(得分:5)

答案 1 :(得分:1)

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&lt; String&gt;,表示:字符串列表。使用new运算符,可以创建实际对象。显然,您必须选择一个实现,并选择“ArrayList”。当然,您希望在集合中使用字符串,因此指定String作为类型参数。由于您正在调用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);

答案 2 :(得分:0)

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

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

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

List是Collection的特化。

  

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

     

所有超级接口:       收藏,Iterable

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

更多信息:

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

答案 3 :(得分:0)

解释如下

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
}