程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

JAVA中ArrayList/LinkedList常用API与注意事项

发布于2020-11-19 20:29     阅读(1379)     评论(0)     点赞(1)     收藏(5)


package Demo;

import java.util.ArrayList;
import java.util.Set;

public class Demo3 {
    public static void main(String[] args) {
        /**
         * ArrayList的注意事项
         * ArrayList是一个数组结构,查询快,增删慢
         * LinkedList是一个链表结构且是双向链表,增删快,查询慢
         * 取值要注意索引越界的问题
         * <>里面是泛型,泛型必须是引用数据类型,添加的数据必须与指定的泛型类型一致
         * 直接打印输出的是list元素,不是地址值,只有数组直接打印的是地址值
         * 若想用基本数据类型,需使用包装类,在Java.lang包下,如下:
         * Byte-Short-Integer-Long-Float-Double-Character-Booleam
         * jdk1.5以后会自动装箱、拆箱
         * 自动装箱:基本类型--->包装类型
         * 自动拆箱:包装类型--->基本类型
         * 
         * ArrayList的常用方法
         * add():添加元素,返回一个布尔值,添加成功为true,失败为false
         * get():获取元素,返回指定的泛型类型
         * remove():删除元素,返回被删除的元素
         * size():获取list长度,也就是list元素个数,返回int类型	
         * isEmpty():判断列表是否为空,为空返回true
         * 遍历:for循环、while循环配合迭代器进行遍历
         * 
         * LinkedList的常用方法
         * addFirst():在列表开头添加元素
         * addLast():在列表末尾添加元素
         * getFirst():获取列表开头元素
         * getLast():获取列表末尾元素
         * removeFirst():删除列表开头元素
         * removeLast():删除列表末尾元素
         * isEmpty():判断列表是否为空,为空返回true
         * clear():清空列表
         */
        ArrayList<String> arrayList = new ArrayList<>();
        //添加
        arrayList.add("张三");
        arrayList.add("李四");
        arrayList.add("wangwu");

        //删除--可根据索引或元素删除
        arrayList.remove(0);
        arrayList.remove("wangwu");

        arrayList.set(1,"lisi");    //修改

        arrayList.get(0);       //查询--根据索引查询,索引从0开始

        arrayList.size();       //获取list长度或个数

        arrayList.isEmpty();    //判断是否为空

        arrayList.clear();      //清空

        arrayList.indexOf("李四");    //获取某个元素的索引位置

        arrayList.contains("张三");   //是否包含某个元素
        
        //练习题目:键盘录入6个整数添加到集合中并遍历集合
        ArrayList<Integer> arrayList1 = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i <6 ; i++) {
            System.out.println("请输入第"+(i+1)+"整数");
            int num = sc.nextInt();
            arrayList1.add(num);
            /**若是6个0-33之内的整数,循环体只写这一个即可
            arrayList1.add(new Random().nextInt(34));
            */
        }
        //遍历
        for (int i = 0; i <arrayList1.size() ; i++) {
            System.out.println(arrayList1.get(i));
        }
        
		
		//练习题:创建4个学生对象并添加到list中
		
		/**  学生对象类
		public class Student {
    	private String name;
   	    private int age;

    	public Student(){};

    	public Student(String name, int age) {
        this.setName(name);
        this.setAge(age);
    	}

    	public String getName() {
        return name;
    	}

    	public void setName(String name) {
        this.name = name;
    	}

    	public int getAge() {
        return age;
    	}

    	public void setAge(int age) {
        this.age = age;
    	}
}
 */
 		//将学生对象添加到list中
		ArrayList<Student> student = new ArrayList<>();
        student.add(new Student("张三",18));
        student.add(new Student("李四",19));
        student.add(new Student("王五",20));
        student.add(new Student("老六",18));

        //for遍历
        for (int i = 0; i <student.size() ; i++) {
            Student1 student = student.get(i);  //获取每一个学生对象
            System.out.println(student.getName()+student.getAge());
        }
		
		//while循环+迭代器遍历
        Iterator<Student> iterator = student.iterator();
        while (iterator.hasNext()){
            Student st = iterator.next();
            System.out.println(st.getName()+st.getAge());
        };
       
    }
}

原文链接:https://blog.csdn.net/weixin_43489515/article/details/109733839



所属网站分类: 技术文章 > 博客

作者:你不要惹我

链接:http://www.javaheidong.com/blog/article/868/163e09d870e3150aed36/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

1 0
收藏该文
已收藏

评论内容:(最多支持255个字符)