Home  >  Article  >  Java  >  A brief introduction to an ordered collection that allows duplication List in Java

A brief introduction to an ordered collection that allows duplication List in Java

php是最好的语言
php是最好的语言Original
2018-08-02 11:55:177541browse

  1. List collection is an ordered collection that allows duplication. Of course, it can also use all methods of Collection

  2. Special It is pointed out that List elements can be accessed through subscripts. The subscript of the first element is 0, and the second one is 1

  3. The following are some examples of Lsit methods

    //List集合是有序的,允许重复的每个元素都有对应的索引,可以通过索引来访问元素
    //索引下标从0开始
    
    
    package List;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class ListTest {
        public static void main(String args[]){
            List list = new ArrayList();
    
            list.add(new String("java1"));
            list.add(new String("java2"));
            list.add(new String("java3"));
    
            list.add(1, new String("java4"));
    //      将下标为1(也就是第二个元素)置为 java4,本来的元素从第二个元素开始全部往后移动
    //      一位
    
    
            for(int i=0 ;i<list.size(); i++){
                System.out.print(list.get(i)  + ",");
    //      通过get(i)(i为下标)可以访问list指定位置的元素
            }
    
           System.out.println();
    
            list.remove(2);
    //      删除指定下标的元素(下标为2即时第三个元素)
    
            System.out.println(list);
    
            System.out.println(list.indexOf(new String("java1")) );
    //      注意,这里indexOf()会返回1,说明indexOf()方法不是通过 == 来匹配元素
    //      而是通过equals()方法来匹配的
    
    
            list.set(1, new String("java5"));
    //      设置特定下标为特定元素
    
            System.out.println(list);
    
            System.out.println(list.subList(1,2));
    //      包头不包尾
    
    
    
        }
    }
    //输出结果
    //        java1,java4,java2,java3,
    //        [java1, java4, java3]
    //        0
    //        [java1, java5, java3]
    //        [java5]

    Related articles:

    List code analysis of Java collections

    JAVA sorts list collections Collections.sort()

    Related videos:

    Geek Academy Java Video Tutorial

The above is the detailed content of A brief introduction to an ordered collection that allows duplication List in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn