zoukankan      html  css  js  c++  java
  • 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;
    }
    }
    }
    }

  • 相关阅读:
    HTML/网站一键打包APK工具(html网页打包安卓APP应用)
    四款最好用的html转苹果APP免上架免签名打包在线生成工具
    ios11,弹出层内的input框光标错位
    前端进阶小知识
    html数据交互小知识
    H5特殊情况解决方案
    H5个人不常用但感觉很重要的东西
    H5小知识
    Java SE 8 Programmer复习小结
    JSP页面中onSubmit方法不执行
  • 原文地址:https://www.cnblogs.com/zaoxi11/p/6026370.html
Copyright © 2011-2022 走看看