package com.list;
import java.util.ArrayList;
import java.util.List;
public class ListTest {
public static void main(String[] args) {
List person = new ArrayList();
person.add("sss");
person.add("hhh");
person.add("eee");
person.add("aaa"); int i = 0;
System.out.println(person);
for (Object o : person) {
System.out.println( person.get(i));
i++;
}
System.out.println(person.indexOf("sss"));
person.add(2,"ggg");
System.out.println(person);
person.set(1,"qqq");
System.out.println(person);
}
}
if List
num.replaceAll(new UnaryOperator() {
@Override
public Object apply(Object o) {
int num = (int) o;
return num*num;
}
});
类型为int,remove(1)当作索引
list 的数据处理和排序
package com.list;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.function.UnaryOperator;
public class ListTest02 {
public static void main(String[] args) {
ArrayList num = new ArrayList();
num.add(2);
num.add(3);
num.add(4);
num.add(5);
num.add(1);
num.add(9);
num.replaceAll(o -> {
int num1 = (int) o;
return num1 * num1;
});
System.out.println(num);
num.sort(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int num1 = (int) o1;
int num2 = (int) o2;
return -1*(num1-num2);
}
});
System.out.println(num);
}
}
remove通过equals来判断是否删除
package com.list;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class ListIteratorTest {
public static void main(String[] args) {
List person = new ArrayList();
person.add("qqq");
person.add("www");
person.add("eee");
person.add("rrrr");
Iterator iterator = person.iterator();
while (iterator.hasNext()){
String ps = (String) iterator.next();
System.out.println(ps);
}
System.out.println("=======================");
ListIterator listIterator = person.listIterator();
while (listIterator.hasNext()){
String ps = (String) listIterator.next();
System.out.println(ps);
}
System.out.println("=======================");
while (listIterator.hasPrevious()){
String ps = (String) listIterator.previous();
System.out.println(ps);
}
}
}