zoukankan      html  css  js  c++  java
  • LinkedListTest

    package ArrayListT;
    
    import java.util.Iterator;
    import java.util.LinkedList;
    
    public class LinkedListTest {
    
        public static void main(String[] args) { 
            LinkedList<String> linkedList = new LinkedList<String>();
            //共有的方法
            linkedList.add("1");
            linkedList.add("2");
            linkedList.add("4");
            linkedList.add("5");
            linkedList.add(2, "3");
            int lenght = linkedList.size();
            System.out.println(lenght);//5
            System.out.println(linkedList.get(2));//3
            System.out.println(linkedList.contains("6"));//false
            linkedList.remove("5");//1,2,3,4
            System.out.println(linkedList.size());//4
            linkedList.remove(0);//2,3,4
            System.out.println(linkedList.contains("1"));//false
            System.out.println(linkedList.indexOf("2"));//0
            System.out.println(linkedList.lastIndexOf("4"));//2
            linkedList.set(0, "0");//0,3,4
            System.out.println(linkedList.contains("2"));//false
            System.out.println(linkedList.indexOf("0"));//0
            //自己的方法
            linkedList.addFirst("-1");//-1,0,3,4
            linkedList.addLast("5");//-1,0,3,4,5
            System.out.println(linkedList.getFirst());//-1
            System.out.println(linkedList.getLast());//5
            linkedList.removeFirst();//0,3,4,5
            System.out.println(linkedList.contains("-1"));//false
            linkedList.removeLast();//0,3,4
            System.out.println(linkedList.contains("5"));//false
            
            //存对象数据
            LinkedList<News> linkedList2 = new LinkedList<News>();
            News news0 = new News("1", "12");
            News news1 = new News("2", "23");
            News news2 = new News("3", "34");
            News news3 = new News("4", "45");
            linkedList2.add(news0);
            linkedList2.add(news1);
            linkedList2.add(news2);
            linkedList2.add(news3);
            //通过for循环加get方法实现
            int count = 0;
            for (int i = 0; i < linkedList2.size(); i++) {
                News news = linkedList2.get(i);
                if(news.title.equals("4")){
                    System.out.println(news.content);
                    break;
                }
                count++;
                if(count == linkedList2.size()){
                    System.out.println("没找到!");
                    return;
                }
            }
            //通过Iterator实现
            int count2 = 0;
            Iterator<News> iterator = linkedList2.iterator();
            while (iterator.hasNext()) {
                News news = iterator.next();
                if(news.title.equals("6")){
                    System.out.println(news.content);
                    count2 = 0;
                }
                count2++;
                if(count2 == linkedList2.size()){
                    System.out.println("未找到!");
                    return;
                }
            }
        }
    }
  • 相关阅读:
    初识python 2.x与3.x 区别
    装饰器
    函数的进阶
    Spring Boot启动问题:Cannot determine embedded database driver class for database type NONE
    22.Spring Cloud Config安全保护
    23.Spring Cloud Bus 无法更新问题(踩坑) Spring cloud config server Could not fetch remote for master remote
    24.Spring Cloud之Spring Cloud Config及Spring Cloud Bus
    Spring Boot整合Spring Data Elasticsearch 踩坑
    项目中Spring Security 整合Spring Session实现记住我功能
    32.再谈SpringBoot文件上传
  • 原文地址:https://www.cnblogs.com/xiaolei121/p/5759102.html
Copyright © 2011-2022 走看看