package practicedemo;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
*
* 1) 创建一个List,在List 中增加三个工人,基本信息如下:
* 姓名 年龄 工资
* zhang3 18 3000
* li4 25 3500
* wang5 22 3200
* 2) 在li4 之前插入一个工人,信息为:姓名:zhao6,年龄:24,工资3300
* 3) 删除wang5 的信息
* 4) 利用for 循环遍历,打印List 中所有工人的信息
* 5) 利用迭代遍历,对List中所有的工人调用work方法。
* 使用 至少六种方式遍历 集合
* 6) 存储重复元素并且去除 (使用两种方式)
*/
public class PracticeDemo02 {
public static void main(String[] args) {
//1) 创建一个List,在List 中增加三个工人,基本信息如下:
// * 姓名 年龄 工资
// * zhang3 18 3000
// * li4 25 3500
// * wang5 22 3200
List<Worker> list = new ArrayList<>();
Worker zhang3 = new Worker("zhang3",18,3000.0);
Worker li4 = new Worker("li4",25,3500);
Worker wang5 = new Worker("wang5",22,3200);
list.add(zhang3);
list.add(li4);
list.add(wang5);
System.out.println("----------1------------");
for(Worker worker : list){
System.out.println(worker);
}
System.out.println("-----------2-----------------");
//2) 在li4 之前插入一个工人,信息为:姓名:zhao6,年龄:24,工资3300
list.add(1,new Worker("zhao6",24,3300));
System.out.println(list);
System.out.println("-------------3---------------");
//3) 删除wang5 的信息
// list.remove(3);
// System.out.println(list);
//list.remove(Object o)
list.remove(wang5);
System.out.println(list);
System.out.println("--------------4-for------------------");
//4) 利用for 循环遍历,打印List 中所有工人的信息
for (int i = 0; i < list.size(); i++) {
Worker worker = list.get(i);
System.out.println(worker);
}
//5) 利用迭代遍历,对List中所有的工人调用work方法。
// * 使用 至少六种方式遍历 集合
System.out.println("----------------遍历1--------------------");
Iterator<Worker> iterator = list.iterator();//获取迭代器对象
while(iterator.hasNext()){
Worker worker = iterator.next();
System.out.println(worker.work());
}
System.out.println("----------------遍历2--------------------");
Object[] objects = list.toArray();
for(Object obj : objects){
Worker worker = (Worker)obj;
System.out.println(worker.work());
}
System.out.println("----------------遍历3--------------------");
Worker[] workers = list.toArray(new Worker[]{});
for(Worker worker : workers){
System.out.println(worker.work());
}
System.out.println("----------------遍历4--------------------");
ListIterator<Worker> listIterator = list.listIterator();
while(listIterator.hasNext()){
Worker worker = listIterator.next();
System.out.println(worker.work());
}
System.out.println("----------------遍历5--------------------");
ListIterator<Worker> listIterator1 = list.listIterator(0);
while(listIterator1.hasNext()){
Worker worker = listIterator1.next();
System.out.println(worker.work());
}
System.out.println("----------------遍历5--------------------");
for( Iterator<Worker> it = list.iterator(); it.hasNext();){
Worker worker = it.next();
System.out.println(worker.work());
}
System.out.println("----------------遍历6--------------------");
for( Iterator<Worker> it = list.iterator(); it.hasNext(); System.out.println(it.next().work())){}
System.out.println("----------------遍历7--------------------");
for(Worker worker : list){
System.out.println(worker.work());
}
System.out.println("----------------遍历8--------------------");
System.out.println(list);
System.out.println("----------------遍历9--------------------");
for (int i = 0; i < list.size(); i++) {
Worker worker = list.get(i);
System.out.println(worker.work());
}
list.add(new Worker("zhao6",24,3300));
System.out.println(list);
//6) 存储重复元素并且去除 (使用两种方式)
List<Worker> newList = new ArrayList<>();
System.out.println("-----------去重1---------------");
for(Worker worker : list){
if (!newList.contains(worker)){
newList.add(worker);
}
}
list = newList;
for(Worker worker : list){
System.out.println(worker.work());
}
System.out.println("-----------去重2---------------");
for (int i = 0; i < list.size() - 1; i++) {
for (int j = i+1; j < list.size(); j++) {
if (list.get(i).equals(list.get(j))){
list.remove(j);
j--;
}
}
}
for(Worker worker : list){
System.out.println(worker.work());
}
}
}
1) 创建一个List,在List 中增加三个工人,基本信息如下: 姓名 年龄 工资 zhang3 18 3000 li4 25 3500 wang5 22 320
最新推荐文章于 2023-01-09 18:59:26 发布