1.去除ArrayList中重复字符串元素方式
* A:案例演示
* 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同)
* 思路:创建新集合方式
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo1_ArrayList {
/**
* * A:案例演示
* 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同)
* 思路:创建新集合方式
*/
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("a");
list.add("a");
list.add("b");
list.add("b");
list.add("c");
list.add("c");
list.add("c");
list.add("c");
ArrayList newList = getSingle(list);
System.out.println(newList);
}
/*
* 创建新集合将重复元素去掉
* 1,明确返回值类型,返回ArrayList
* 2,明确参数列表ArrayList
*
* 分析:
* 1,创建新集合
* 2,根据传入的集合(老集合)获取迭代器
* 3,遍历老集合
* 4,通过新集合判断是否包含老集合中的元素,如果包含就不添加,如果不包含就添加
*/
public static ArrayList getSingle(ArrayList list){
ArrayList newList=new ArrayList<>();
Iterator it=list.iterator();
while(it.hasNext()){
Object obj=it.next();
if(!newList.contains(obj)){
newList.add(obj);
}
}
return newList;
}
}
2.LinkedList的特有功能
* A:LinkedList类概述
* B:LinkedList类特有功能
* public void addFirst(E e)及addLast(E e)
* public E getFirst()及getLast()
* public E removeFirst()及public E removeLast()
* public E get(int index);
3.栈和队列数据结构
* 栈
* 先进后出
* 队列
* 先进先出
4.用LinkedList模拟栈数据结构的集合并测试
* A:案例演示
* 需求:请用LinkedList模拟栈数据结构的集合,并测试
* 创建一个类将Linked中的方法封装
public class Stack{
private LinkedList list=new LinkedList(); //创建LinkedList对象
public void int(Object obj){ //封装addLast()方法
list.addLast(obj);
}
public Object out(){ //封装removeLast()方法
return list.removeLast();
}
public boolean isEmpty(){ //封装isEmpty()方法
return list.isEmpty();
}
}
5.泛型概述和基本使用
* A:泛型概述
* B:泛型好处
* 提高安全性(将运行期的错误转换到编译期)
* 省去强转的麻烦
* C:泛型基本使用
* <>中放的必须是引用数据类型
* D:泛型使用注意事项
* 前后的泛型必须一致,或者后面的泛型可以省略不写(1.7的新特性菱形泛型)
6.泛型高级之通配符
* A:泛型通配符<?>
* 任意类型,如果没有明确,那么就是Object以及任意的Java类了
* B:? extends E
* 向下限定,E及其子类
* C:? super E
* 向上限定,E及其父类
7.ArrayList存储字符串和自定义对象并遍历增强for版
* A:案例演示
* ArrayList存储字符串并遍历增强for版
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
for(String s : list) {
System.out.println(s);
}
8.三种迭代的能否删除
* 普通for循环,可以删除,但是索引要--
* 迭代器,可以删除,但是必须使用迭代器自身的remove方法,否则会出现并发修改异常
* 增强for循环不能删除
9.静态导入的概述和使用
* A:静态导入概述
* B:格式:
* import static 包名….类名.方法名;
* 可以直接导入到方法的级别
* C:注意事项
* 方法必须是静态的,如果有多个同名的静态方法,容易不知道使用谁?这个时候要使用,必须加前缀。由此可见,意义不大,所以一般不用,但是要能看懂。
10.可变参数的概述和使用
* A:可变参数概述
* 定义方法的时候不知道该定义多少个参数
* B:格式
* 修饰符 返回值类型 方法名(数据类型… 变量名){}
* C:注意事项:
* 这里的变量其实是一个数组
* 如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个
/**
* A:可变参数概述
* 定义方法的时候不知道该定义多少个参数
* B:格式
* 修饰符 返回值类型 方法名(数据类型… 变量名){}
* C:注意事项:
* 这里的变量其实是一个数组
* 如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个
*/
public class ChangeableArgs{
public static void main(String[] args){
int arr={11,22,33,44,55};
print(11,22,33,44,55);
}
public static void print(int...arr){ //可变参数其实是一个数组
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}
11.Arrays工具类的asList()方法的使用
* A:案例演示
* Arrays工具类的asList()方法的使用
* Collection中toArray(T[] a)泛型版的集合转数组
/**
* 数组转换成集合
* 数组转换成集合虽然不能增加或减少元素,但是可以用集合的思想操作数组,也就是说可以使用其他集合中的方法
*/
ArrayList<String> list=new ArrayList<>();
list.add("a");
list.add("b");
String[] arr=list.toArray(new String[10]);
//当集合转换数组时,数组长度如果小于等于集合的size时,转换后的数组长度等于集合的size
//如果数组的长度大于了size,分配的数组长度就和你指定的长度一样
for(String str:arr){
System.out.println(string);
}
String[] arr={"a","b","c"};
List<String> list=Arrays.asList(arr);
System.out.println(list);
12.集合嵌套之ArrayList嵌套ArrayList
public class Demo5_ArrayListArrayList {
/**
* * A:案例演示
* 集合嵌套之ArrayList嵌套ArrayList
* 案例:
* 我们学科,学科又分为若个班级
* 整个学科一个大集合
* 若干个班级分为每一个小集合
*/
public static void main(String[] args) {
ArrayList<ArrayList<Person>> list = new ArrayList<>();
ArrayList<Person> first = new ArrayList<>(); //创建第一个班级
first.add(new Person("杨幂", 30));
first.add(new Person("李冰冰", 33));
first.add(new Person("范冰冰", 20));
ArrayList<Person> second = new ArrayList<>();
second.add(new Person("黄晓明", 31));
second.add(new Person("赵薇", 33));
second.add(new Person("陈坤", 32));
//将班级添加到学科集合中
list.add(first);
list.add(second);
//遍历学科集合
for(ArrayList<Person> a : list) {
for(Person p : a) {
System.out.println(p);
}
}
}
}
Person [name=杨幂, age=30]
Person [name=李冰冰, age=33]
Person [name=范冰冰, age=20]
Person [name=黄晓明, age=31]
Person [name=赵薇, age=33]
Person [name=陈坤, age=32]