Java容器---List
List 承诺可以将元素维护在特定的序列中。List 接口在Collection的基础上添加了大量的方法,使得可以在List的中间插入和移除元素。
有两种类型的List:
—–基本的ArrayList,它长于随机访问元素,但是在List的中阅插入和移除元素时较慢;
—–LinkedList,它通过代价较低的在List中间进行的插入和删除操作(链表的特性),提供了优化的顺序访 问。LinkedList在随机访问方面相对比较慢,但是它的特性集较ArrayList 更大。
(1)代码1:演示List特有方法
1 public class ListDemo { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 8 List list = new ArrayList(); 9 methodDemo(list); 10 } 11 /* 12 * 演示List特有的方法。 13 */ 14 public static void methodDemo(List list){ 15 16 //1,常规添加元素。 17 list.add("abc1"); 18 list.add("abc2"); 19 list.add("abc3"); 20 21 //2,插入元素。 22 // list.add(1,"hehe"); 23 24 //3,删除。 25 // list.remove(1); 26 27 //4,获取。 28 // System.out.println(list.get(1)); 29 // System.out.println(list.indexOf("abc3")); 30 31 //5,修改。 32 // list.set(1, "kk"); 33 34 35 36 //取出集合中所有的元素。 37 for (Iterator it = list.iterator(); it.hasNext();) { 38 System.out.println("iterator:"+it.next()); 39 } 40 41 //list集合特有的取出方式。 42 for (int i = 0; i < list.size(); i++) { 43 System.out.println("get:"+list.get(i)); 44 } 45 46 // System.out.println(list); 47 48 49 } 50 }
(2)代码2:迭代器listIterator()演示
1 public class ListIteratorDemo { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 8 List list = new ArrayList(); 9 10 list.add("abc1"); 11 list.add("abc2"); 12 list.add("abc3"); 13 list.add("abc4"); 14 15 16 /* 17 //在遍历的过程中,如果遍历到abc2,添加一个元素haha 18 for (Iterator it = list.iterator(); it.hasNext();) { 19 Object obj = it.next();//java.util.ConcurrentModificationException 20 21 //迭代过程中使用了使用了集合对象同时对元素进行操作。导致了迭代的不确定性。引发了该异常。 22 //解决思想:在迭代过程中,想要执行一些操作,使用迭代器的方法就可以了。 23 24 if(obj.equals("abc2")){ 25 list.add("haha"); 26 } 27 }*/ 28 29 // 使用list集合的特有的迭代器。ListIterator 通过List集合的方法listIterator()获取该迭代器对象。 30 // ListIterator可以实现在迭代过程中的增删改查。 31 for (ListIterator it = list.listIterator(); it.hasNext();) { 32 Object obj = it.next(); 33 34 if(obj.equals("abc2")){ 35 it.add("haha"); 36 } 37 38 } 39 40 System.out.println(list); 41 } 42 43 } 44
(3)代码3:LinkedList方法演示
1 public class LinkedListDemo { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 8 //1,创建一个链表对象。//演示 XXXFirst XXXLast。 9 LinkedList link = new LinkedList(); 10 11 //2,添加方法。 12 link.addFirst("abc1"); 13 link.addFirst("abc2"); 14 link.addFirst("abc3"); 15 16 //3,获取元素。 17 // System.out.println(link.getFirst()); 18 // System.out.println(link.getFirst()); 19 20 //4,删除元素。 21 // System.out.println(link.removeFirst()); 22 // System.out.println(link.removeFirst()); 23 24 //取出link中所有元素。 25 while(!link.isEmpty()) 26 System.out.println(link.removeLast()); 27 28 } 29 30 }
(4)通过LinkedList实现一个堆栈,或者队列数据结构。
1 public class LinkedListTest { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 8 /* 9 * 练习: 请通过LinkedList实现一个堆栈,或者队列数据结构。 10 * 堆栈: 先进后出。 First In Last Out FILO. 11 * 队列: 先进先出。First In First Out FIFO. 12 * 13 */ 14 15 //1,创建自定义的队列对象。 16 MyQueue queue = new MyQueue(); 17 18 //2,添加元素。 19 queue.myAdd("abc1"); 20 queue.myAdd("abc3"); 21 queue.myAdd("abc4"); 22 queue.myAdd("abc5"); 23 24 //3,获取所有元素。先进先出。 25 while(!queue.isNull()){ 26 System.out.println(queue.myGet()); 27 } 28 29 } 30 31 } 32 33 /* 34 * 描述一个队列数据结构。内部使用的是LinkedList。 35 */ 36 class MyQueue{ 37 38 private LinkedList link; 39 MyQueue(){ 40 link = new LinkedList(); 41 } 42 43 /** 44 * 添加元素的方法。 45 */ 46 public void myAdd(Object obj){ 47 //内部使用的是linkedlist的方法。 48 link.addFirst(obj); 49 } 50 51 /** 52 * 获取队列元素的方法。 53 */ 54 public Object myGet(){ 55 return link.removeLast(); 56 } 57 58 /** 59 * 集合中是否有元素的方法。 60 */ 61 public boolean isNull(){ 62 return link.isEmpty(); 63 } 64 } 65 66
2018-01-03
内容来自传智播客和java编程思想