有关list容器的详细用法
来源:互联网 发布:滨州行知中学招聘 编辑:程序博客网 时间:2024/04/26 05:44
1、定义一个Person类 name 和 age两属性
2、循环创建n(假设为5)个person对象,(
name person1 age 10
name person2 age 20
name person3 age 30
name person4 age 40
name person5 age 50
)
3、分别添加到list容器对象中
4、删除第5个位置的对象
5、把第4个位置的值设置为新Person对象(name person444 age444),
6、把list转换为Person[]数组,使用Arrays的toString打印元素信息
7、三种方式遍历list对象
a)普通for
b)增强for
2、循环创建n(假设为5)个person对象,(
name person1 age 10
name person2 age 20
name person3 age 30
name person4 age 40
name person5 age 50
)
3、分别添加到list容器对象中
4、删除第5个位置的对象
5、把第4个位置的值设置为新Person对象(name person444 age444),
6、把list转换为Person[]数组,使用Arrays的toString打印元素信息
7、三种方式遍历list对象
a)普通for
b)增强for
c)Iterator
import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;import java.util.List;public class Test3 {public static void main(String[] args) {List<Person> list=new ArrayList<Person>();Person p1=new Person("person1",10);Person p2=new Person("person2",20);Person p3=new Person("person3",30);Person p4=new Person("person4",40);Person p5=new Person("person5",50);list.add(p1);list.add(p2);list.add(p3);list.add(p4);list.add(p5);list.remove(4);Person quchu=list.get(3);quchu.name="person444";quchu.age=444;Person[] p=new Person[list.size()];list.toArray(p);System.out.println(Arrays.toString(p));for(int i=0;i<list.size();i++){Person temp=(Person) list.get(i);System.out.println(temp);}for(Person temp:list){System.out.println(temp);}for(Iterator iter=list.iterator();iter.hasNext();){Person temp=(Person) iter.next();System.out.println(temp);}}}class Person{String name;int age;public Person(String name,int age){super();this.name=name;this.age=age;}public String toString(){return name+" "+age;}}
0 0
- 有关list容器的详细用法
- list 容器的用法
- list 容器的用法
- 【C++】容器list的用法
- map容器的详细用法
- 有关STL中的set容器的用法
- c++中容器list的用法整理
- STL — list容器用法的详解
- C++--vector<>容器,List<>链表,map<>容器的用法
- vector 容器详细用法
- C# List<T>的详细用法
- C# List T 的详细用法
- C# List<T>的详细用法
- 有关类ListCtrl 详细用法
- 有关C#中的List的用法集合(仅是个人统计)
- 有关List中pushback()的用法及释放注意事项
- 顺序容器STL::list用法
- List容器用法操作实例
- JAXB教程
- iOS开发(第三方使用)——百度地图鹰眼轨迹SDK接入
- Ipython出现Warning的解决方案
- Matrix4x4.MultiplyVector 变换方向
- web整站https环境下使用(spring mvc)redirect重定向到http协议问题
- 有关list容器的详细用法
- Remove Duplicates from Sorted List II 删除链表中的所有重复节点
- 在Cocos2dx 3.10中第一次使用shader
- C++文件操作
- c语言程序设计感受
- 使用git客户端软件GitHub Desktop来管理项目
- linux socket的IO多路复用简单例子(一)
- 第15周阅读程序5
- git的使用