[AppleScript] 纯文本查看 复制代码
public class Demo {
public static void main(String[] args) {
//删除所有姓张的元素
ArrayList<String> list = new ArrayList<>();
list.add("张无忌");
list.add("张三丰");
list.add("赵敏");
list.add("周芷若");
list.add("张翠山");
for (int i = list.size() - 1; i >= 0; i--) {
if (list.get(i).startsWith("张")){
list.remove(i);
}
}
System.out.println(list);
}
}
[AppleScript] 纯文本查看 复制代码
public class Atest {
int sum = 0;
List<Integer> strList = createList();
public static void main(String[] args) {
new Atest().mytest();
}
/**
* 多线程计算list中数值的和
* 测试spliterator遍历`
*/
public void mytest() {
Spliterator<Integer> spliterator01 = strList.spliterator();
Spliterator<Integer> spliterator02 = spliterator01.trySplit();
Spliterator<Integer> spliterator03 = spliterator01.trySplit();
Spliterator<Integer> spliterator04 = spliterator02.trySplit();
MyThread t01 = new MyThread(spliterator01);
MyThread t02 = new MyThread(spliterator02);
MyThread t03 = new MyThread(spliterator03);
MyThread t04 = new MyThread(spliterator04);
t01.start();
t02.start();
t03.start();
t04.start();
//等所有的线程都执行完成后输出计算结果
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("结果为:" + sum);
}
class MyThread extends Thread {
private Spliterator<Integer> list;
public MyThread(Spliterator<Integer> list) {
this.list = list;
}
@Override
public void run() {
String threadName = Thread.currentThread().getName();
System.out.println("线程" + threadName + "开始运行-----");
list.forEachRemaining(num -> {
sum += num;
System.out.println(num + " " + threadName);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}
private static List<Integer> createList() {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < 10; i++) {
result.add(i);
}
System.out.println(result);
return result;
}
}