• 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;
                }
            }
        }
    }
  • 相关阅读:
    Docker的使用
    Django常见问题
    Linux系统使用
    Nginx
    Redis
    MySQL基础、主从复制、优化
    Python常见的问题
    Python基础知识
    Vue的使用
    python监控tomcat日记文件
  • 原文地址:https://www.cnblogs.com/xiaolei121/p/5759102.html
Copyright © 2020-2023  润新知