• 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;
                }
            }
        }
    }
  • 相关阅读:
    基于TMS320C6678、FPGA XC5VLX110T的6U CPCI 8路光纤信号处理卡
    KC705E 增强版 基于FMC接口的Xilinx Kintex7 FPGA K7 XC7K325T PCIeX8 接口卡
    基于Xilinx Kintex7 FPGA K7 XC7K325T PCIeX8 四路光纤卡
    IOS常用API
    数学和物理的公式
    [cocos2d]如何实现模态对话框
    使用加速器实现的定位算法
    cocos2d 粒子设计器
    陀螺仪操作摄相机,可以旋转,但角度(轴)还不对
    gluLookAt 的参数
  • 原文地址:https://www.cnblogs.com/xiaolei121/p/5759102.html
Copyright © 2020-2023  润新知