package com.cloud.list; import java.util.ArrayList; import java.util.List; public class Demo1 { public static void main(String[] args) { List list=new ArrayList(); list.add("Spring1"); list.add("Spring2"); list.add("Spring3"); list.add("Spring4"); for(int i=0;i<list.size();i++){ System.out.println(list.get(i)+";"); } List sublist=list.subList(1, 4); System.out.println(sublist); } }
package com.cloud.list; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class Demo2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub List list=new ArrayList(); list.add("Spring1"); list.add("Spring2"); list.add("Spring3"); ListIterator it=list.listIterator(); it.next(); it.next(); it.set("Spring5"); /* it.add("Spring4"); while(it.hasNext()){ System.out.println(it.next()); } */ System.out.println(list); } }
package com.cloud.list; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class Demo3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub List list=new ArrayList(); list.add("Spring1"); list.add("Spring2"); list.add("Spring3"); //一:for变量集合 for(int i=0;i<list.size();i++){ System.out.print(list.get(i)+";"); } System.out.println(); //二:迭代器正序迭代 ListIterator it=list.listIterator(); while(it.hasNext()){ System.out.print(it.next()+";"); } System.out.println(); //三:迭代器倒序迭代 while(it.hasPrevious()){ System.out.print(it.previous()+";"); } } }
package com.cloud.list; import java.util.ArrayList; import java.util.Iterator; /* ArrayList * 查询快,增删慢 */ public class Demo4 { public static void main(String[] args) { ArrayList list=new ArrayList(); list.add(new Book(1,"java编程1")); list.add(new Book(2,"java编程2")); list.add(new Book(3,"java编程3")); list.add(new Book(3,"java编程4")); System.out.println(list); ArrayList list2=clearRepeat(list); System.out.println(list2); } public static ArrayList clearRepeat(ArrayList list){ //创建新的集合 ArrayList newList=new ArrayList(); //获取新的集合 Iterator it= list.iterator(); while(it.hasNext()){ Book book=(Book) it.next(); if(!newList.contains(book)){ newList.add(book); } } return newList; } } class Book{ int id; String name; public Book(int id,String name){ this.id=id; this.name=name; } @Override public String toString() { // TODO Auto-generated method stub return "(编号:"+this.id+";名字:"+this.name+")"; } @Override public boolean equals(Object obj) { Book book=(Book)obj; return this.id==book.id; } @Override public int hashCode() { // TODO Auto-generated method stub return super.hashCode(); } }
package com.cloud.list; import java.util.Iterator; import java.util.LinkedList; public class Demo5 { /* LinkedList 查询速度慢,增删快 Linkedlist特有的方法: 1:方法介绍 addFirst(E e) addLast(E e)
getFirst() getLast()
removeFirst() removeLast()
2:数据结构 1:栈 (1.6) : 主要是用于实现堆栈数据结构的存储方式。 先进后出 push() pop() 2:队列(双端队列1.5): 主要是为了让你们可以使用LinkedList模拟队列数据结构的存储方式。 先进先出 offer() poll()
3:返回逆序的迭代器对象 descendingIterator() 返回逆序的迭代器对象 */ public static void main(String[] args) { LinkedList list=new LinkedList(); list.add("Spring2"); list.add("Spring3"); list.add("Spring4"); // list.addFirst("Spring1"); // list.push("Spring");//把该元素放到集合的首位置 // System.out.println("集合的首元素"+list.pop());//移除并返回集合中的第一个元素 // list.offer("Spring0");//添加到末尾位置 // System.out.println(list);
//逆向输出的迭代器 Iterator it = list.descendingIterator(); while (it.hasNext()) { System.out.println(it.next()); } }
} |
package com.cloud.list;
import java.util.LinkedList;
public class Demo6 { /* 2:数据结构 1:栈 (1.6) : 主要是用于实现堆栈数据结构的存储方式。 先进后出 push() pop() 2:队列(双端队列1.5): 主要是为了让你们可以使用LinkedList模拟队列数据结构的存储方式。 先进先出 offer() poll() */ public static void main(String[] args) { System.out.println("======测试值栈======="); StackList list=new StackList(); list.add("Spring1"); list.add("Spring2"); list.add("Spring3"); // System.out.println(list.pop());//删除并返回Spring3 // System.out.println(list.pop());//删除并返回Spring2 // System.out.println(list.pop());//删除并返回Spring1
/*list.size()大小在不断的变化,所以很难遍历完 for(int i=0;i<list.size();i++){ System.out.println(list.pop()); } */ int size=list.size(); for(int i=0;i<size;i++){ System.out.println(list.pop()); } System.out.println("======测试队列======="); TeamList list2=new TeamList(); list2.add("Spring1"); list2.add("Spring2"); list2.add("Spring3"); int size2=list2.size(); for(int i=0;i<size2;i++){ System.out.println(list2.remove()); } } } //模拟堆栈存储方式 class StackList{ LinkedList list; public StackList(){ list=new LinkedList(); } // 进栈 public void add(Object o){ list.push(o); } //出栈 public Object pop(){ return list.pop(); } //大小 public int size(){ return list.size(); } } //模拟队列存储方式 class TeamList{ LinkedList list; public TeamList(){ list=new LinkedList(); } public void add(Object o){ list.offer(o); } public Object remove(){ return list.poll(); } public int size(){ return list.size(); } } |
package com.cloud.day3; import java.util.LinkedList; import java.util.Random; /* * 实现一副扑克牌的洗牌功能 */ class Poker{ String color; String num; public Poker(String color,String num){ this.color=color; this.num=num; } public String toString(){ return "{"+color+num+"}"; } } public class Demo1 { public static void main(String[] args) { LinkedList<Poker> list=createPokers(); System.out.println(list.size()+"张扑克牌"); washPokers(list); showPokers(list); } //获取一副扑克牌 public static LinkedList<Poker> createPokers(){ LinkedList<Poker> list=new LinkedList<Poker>(); String [] color={"红桃","黑桃","方块","梅花"}; String [] nums={"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; for(int i=0;i<nums.length;i++){ for(int j=0;j<color.length;j++){ list.add(new Poker(color[j],nums[i])); } } return list; } //显示一副扑克牌 public static void showPokers(LinkedList<Poker> list){ for(int i=0;i<list.size();i++){ System.out.print(list.get(i)); if(i%10==9){ System.out.println(); } } } //刷新一副扑克牌 public static void washPokers(LinkedList<Poker> pokers){ Random random=new Random(); for(int i=0;i<20;i++){ //随机产生两个索引值 int index1=random.nextInt(pokers.size()); int index2=random.nextInt(pokers.size()); //根据索引取出两张牌,然后交换顺序 Poker poker1=pokers.get(index1); Poker poker2=pokers.get(index2); pokers.set(index1, poker2); pokers.set(index2, poker1); } } }
package com.cloud.day3; import java.util.LinkedList; public class Demo2 { public static void main(String[] args) { LinkedList<Person> list=new LinkedList<Person>(); list.add(new Person("Spring1",13)); list.add(new Person("Spring2",12)); list.add(new Person("Spring3",11)); for(int i=0;i<list.size()-1;i++){ for(int j=i+1;j<list.size();j++){ Person p1 = list.get(i); Person p2 = list.get(j); if(p1.age>p2.age){ list.set(i, p2); list.set(j, p1); } } } System.out.println(list); } } class Person{ String name; int age; public Person(String name,int age){ this.name=name; this.age=age; } @Override public String toString() { return "[姓名:"+this.name+";年龄:"+this.age+"]"; } }