赞
踩
1)List集合类中元素是有序的(即添加顺序和取出顺序一致),且可以重复;
2)List集合类中的每个元素都有其对应的顺序索引,即支持索引;
3)List容器中的元素对应一个整数型的序号,记载其在容器中的索引位置,可以根据序号存取容器中的元素;
4)List接口实现类有:AbstractList类、AbstractSequentialList类、ArrayList类、AttributeList类、CopyOnWriteArrayList类、LinkedList类、RoleList类、Stack类、RoleUnresolvedList类、Vector类。
- package com.pero.list_;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * @author Pero
- * @version 1.0
- */
- public class List_ {
- public static void main(String[] args) {
-
- List list = new ArrayList();
-
- //1.List集合类中的元素有序(添加和取出顺序一致)、且可以重复;
- list.add("tom");
- list.add("jake");
- list.add("marry");
- list.add("smith");
- System.out.println(list);
-
- //2.List集合中的每个元素都有其对应的顺序索引,支持索引
- System.out.println(list.get(1));
-
- //3.List集合都对应一个整数型的序号,记载其在容器中的位置,可以根据序号存取容器中的元素
- list.set(3,"lucy");
- System.out.println(list.get(3));
-
- //4.List接口的实现类有:AbstractList类、AbstractSequentialList类、ArrayList类、
- // AttributeList类、CopyOnWriteArrayList类、LinkedList类、RoleList类、Stack类、
- // RoleUnresolvedList类、Vector类
- }
- }
- package com.pero.list_;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * @author Pero
- * @version 1.0
- */
- public class ListMethod {
- public static void main(String[] args) {
-
- List list = new ArrayList();
- list.add("jake");
- list.add("lucy");
-
- //void add(int index, Object object)方法:在index位置,插入object元素
- list.add(1,"smith");
- System.out.println(list);
-
- //boolean addAll(int index, Collection collection)方法,从index位置,
- // 开始将collection中的所有元素添加进来
- List list2 = new ArrayList();
- list2.add("pero");
- list2.add("tom");
- list.addAll(2,list2);
- System.out.println(list);
-
- //Object get(int index):获取指定index位置的元素
- System.out.println(list.get(2));
-
- //int indexOf(Object object):返回object元素在集合中首次出现的位置索引
- System.out.println(list.indexOf("tom"));
-
- //int lastIndexOf(Object object):返回object元素在当前集合中末次出现的位置索引
- list.add("jake");
- System.out.println(list.lastIndexOf("jake"));
-
- //Object remove(int Index):移除指定index索引位置的元素,并返回该元素
- System.out.println(list.remove(0));
- System.out.println(list);
-
- //Object set(int index, Object object):设置指定index索引位置的元素为object,相当于替换
- list.set(1,"jake");
- System.out.println(list);
-
- //List subList(int formIndex, int toIndex):
- // 返回从formIndex到toIndex索引位置的子集合
- //[0,3) 0 <= 返回的子集合索引 < 3
- System.out.println(list.subList(1,3));
-
- }
- }
- package com.pero.list_;
-
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
-
- /**
- * @author Pero
- * @version 1.0
- */
- public class ListExercise {
- public static void main(String[] args) {
-
- List list = new ArrayList();
- for (int i = 1; i < 13; i++) {
- list.add("hello"+i);
- }
-
- //在第二个元素位置插入pero
- list.add(1,"pero");
-
- //获取第五个元素
- System.out.println(list.get(4));
-
- //删除第六个元素
- System.out.println(list.remove(5));
- System.out.println(list);
-
- //修改第七个元素
- list.set(6,"三国演义");
-
- //使用迭代器遍历
- Iterator iterator = list.iterator();
- while (iterator.hasNext()) {
- Object object = iterator.next();
- System.out.println(object);
- }
-
-
- }
- }
- package com.pero.list_;
-
- import java.util.*;
-
- /**
- * @author Pero
- * @version 1.0
- */
- public class ListFor {
- public static void main(String[] args) {
-
- List list = new ArrayList();
- //List list = new Vector();
- //List list = new LinkedList();
-
- list.add("jake");
- list.add("鱼香肉丝");
- list.add("tom");
- list.add("北京烤鸭");
-
- System.out.println(list);
-
- //迭代器遍历
- Iterator iterator = list.iterator();
- while (iterator.hasNext()) {
- Object object = iterator.next();
- System.out.println(object);
- }
-
- //增强for(快捷键大写的I)
- for (Object o :list) {
- System.out.println(o);
- }
-
- //普通for()
- for (int i = 0; i < list.size(); i++) {
- System.out.println(list.get(i));
- }
-
-
- }
- }
- package com.pero.list_;
-
- import java.util.*;
-
- /**
- * @author Pero
- * @version 1.0
- */
- public class ListEx {
- public static void main(String[] args) {
-
- //List接口的其他实现子类一样可以
- //List list = new ArrayList();
- //List list = new Vector();
- List list = new LinkedList();
-
- list.add(new Book("红楼梦",100,"曹雪芹"));
- list.add(new Book("西游记",80,"吴承恩"));
- list.add(new Book("三国",60,"罗贯中"));
- list.add(new Book("水浒传",130,"施耐庵"));
- list.add(new Book("西游记",60,"吴承恩"));
-
- //调用sort()方法进行排序
- sort(list);
-
- //使用迭代器遍历
- Iterator iterator = list.iterator();
- while(iterator.hasNext()){
- Object object = iterator.next();
- System.out.println(object);
- }
-
- //使用增强for进行遍历
- for (Object o :list) {
- System.out.println(o);
- }
-
- //使用普通for进行遍历
- for (int i = 0; i < list.size(); i++) {
- System.out.println(list.get(i));
- }
-
- }
-
- //排序方法
- public static void sort(List list){
- for (int i = 0; i < list.size() -1; i++) {
- for (int j = 0; j < list.size() -1 -i; j++) {
- //取出对象
- Book book1 = (Book) list.get(j);
- Book book2 = (Book) list.get(j+1);
- //指定对象属性排序要求
- if (book1.getPrice() < book2.getPrice()){
- //对集合对应位置的对象按照要求进行替换
- list.set(j,book2);
- list.set(j+1,book1);
- }
- }
- }
- }
- }
- class Book{
- private String name;
- private double price;
- private String author;
-
- public Book(String name, double price, String author) {
- this.name = name;
- this.price = price;
- this.author = author;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public double getPrice() {
- return price;
- }
-
- public void setPrice(double price) {
- this.price = price;
- }
-
- public String getAuthor() {
- return author;
- }
-
- public void setAuthor(String author) {
- this.author = author;
- }
-
- @Override
- public String toString() {
- return "名称:" + name + "\t\t价格" + price + "\t\t作者:" + author;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。