java list解析,Java List集合方法及遍历过程代码解析
发布日期:2021-06-24 16:52:42 浏览次数:3 分类:技术文章

本文共 870 字,大约阅读时间需要 2 分钟。

bdd7f1ff726e

集合元素框架

public class ListDemo02 {

public static void main(String[] args) {

//创建集合对象

List list = new ArrayList();

//添加元素

list.add("hello");

list.add("world");

list.add("java");

//输出集合对象

System.out.println(list); //[hello, world, java]

}

}

方法运行实例

//void add(int index, E element) : 在此集合中的指定位置插入指定的元素

list.add(1,"javaee"); //[hello, javaee, world, java]

list.add(11,"j2ee"); //IndexOutOfBoundsException

//E remove(int index):删除指定索引处的元素,返回被删除的元素

System.out.println(list.remove(1));

/*

world

[hello, java]

*/

//E set(int index,E element):修改指定索引处的元素,返回被修改的元素

System.out.println(list.set(1,"javaee"));

/*

world

[hello, javaee, java]

*/

//E get(int index):返回指定索引处的元素

System.out.println(list.get(1));

/*

world

[hello, world, java]

*/

//用for循环改进遍历

for (int i = 0; i

String s = list.get(i);

System.out.println(s);

}

/*

hello

world

java

[hello, world, java]

*/

List集合是带索引的集合,要考虑越界问题。

bdd7f1ff726e

转载地址:https://blog.csdn.net/weixin_33960567/article/details/115993305 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:matlab 微分符号,Matlab 符号微积分
下一篇:php中页面平滑回到顶部代码,JavaScript简单实现网页回到顶部功能

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月04日 01时33分29秒