• 2018-10-19 00:13:35 ArrayList


    1. 获取集合元素的长度用的是size方法。
    2. 传入Object类型的值,返回boolean值的remove方法,含义是判断是否删除成功。
    3. 传入索引值的remove方法,返回的是被删除的元素。
    4. 修改值得set方法,第一个参数是索引值,第二个参数是修改后的值最后返回的是被修改的值。
    package com.itheima_01;
    
    import java.util.ArrayList;
    
    /*
     * 获取元素
     *         public E get(int index):返回指定索引处的元素
     * 集合长度
     *         public int size():返回集合中的元素的个数
     * 删除元素
     *         public boolean remove(Object o):删除指定的元素,返回删除是否成功
     *         public E remove(int index):删除指定索引处的元素,返回被删除的元素
     * 修改元素
     *         public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
     */
    public class ArrayListDemo2 {
        public static void main(String[] args) {
            //创建集合对象
            ArrayList<String> array = new ArrayList<String>();
            
            //添加元素
            array.add("hello");
            array.add("world");
            array.add("java");
            
            //public E get(int index):返回指定索引处的元素
            //System.out.println("get:"+array.get(0));
            //System.out.println("get:"+array.get(1));
            //System.out.println("get:"+array.get(2));
            
            //public int size():返回集合中的元素的个数
            //System.out.println("size:"+array.size());
            
            //public boolean remove(Object o):删除指定的元素,返回删除是否成功
            //System.out.println("remove:"+array.remove("world"));//true
            //System.out.println("remove:"+array.remove("world"));//false
            
            //public E remove(int index):删除指定索引处的元素,返回被删除的元素
            //System.out.println("remove:"+array.remove(0));
            
            //public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
            System.out.println("set:"+array.set(1, "android"));
            
            //输出
            System.out.println("array:"+array);
        }
    }

    集合的遍历是通过size()方法和get()方法结合使用。2018-10-19 00:13:35

  • 相关阅读:
    审核系统
    ehcache 缓存
    tomcat 内存设置
    html5 开发 跨平台 桌面应用
    service thread 结合使用
    html5桌面应用
    鼠标 事件
    服务器 判断 客户端 文件下载
    使用github管理Eclipse分布式项目开发
    uub代码
  • 原文地址:https://www.cnblogs.com/lzp123456-/p/9814034.html
Copyright © 2020-2023  润新知