0
点赞
收藏
分享

微信扫一扫

ArrayList集合常用方法

waaagh 2022-02-17 阅读 62

ArrayList集合常用方法

1、public E get(int index) 返回指定索引处的元素
2、public int size() 返回集合中的元素的个数
3、public E remove(int index) 删除指定索引处的元素,返回被删除的元素
4、public boolean remove(Object o) 删除指定的元素,返回删除是否成功
5、public E set(int index,E element) 修改指定索引处的元素,返回被修改的元素

实例

package com.xxf1.arraylist;

import java.util.ArrayList;

public class ArrayListDemo1 {
    public static void main(String[] args) {
        ArrayList<String> list=new ArrayList<>();
        list.add("xxf");
        list.add("java");
        list.add("mysql");
        list.add("hahaha");


        //public E get(int index) 返回指定索引处的元素
         String e=list.get(2);
         System.out.println(e);
        //public int size() 返回集合中的元素的个数
        int c=list.size();
        System.out.println(c);
        //完成集合的遍历
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        //public E remove(int index) 删除指定索引处的元素,返回被删除的元素
        System.out.println(list);
         String e1=  list.remove(2);
        System.out.println(e1);
        System.out.println(list);
        //public boolean remove(Object o) 删除指定的元素,返回删除是否成功
        System.out.println(list.remove("hahaha"));
        System.out.println(list);
        //public E set(int index,E element) 修改指定索引处的元素,返回被修改的元素
        String e3=list.set(0,"xxf");
        System.out.println(e3);
    }
}

举报

相关推荐

0 条评论