java 基础16

01_集合框架(去除ArrayList中重复字符串元素方式)
* A:案例演示
* 需求: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("b");
list.add("c");
list.add("c");
list.add("c");
list.add("c");

System.out.println(list);
ArrayList newList = getSingle(list);
System.out.println(newList);
}

/*
* 去除重复
* 1,返回ArrayList
* 2,参数列表ArrayList
*/
public static ArrayList getSingle(ArrayList list) {
ArrayList newList = new ArrayList(); //创建一个新集合
Iterator it = list.iterator(); //获取迭代器
while(it.hasNext()) { //判断老集合中是否有元素
String temp = (String)it.next(); //将每一个元素临时记录住
if(!newList.contains(temp)) { //如果新集合中不包含该元素
newList.add(temp); //将该元素添加到新集合中
}
}
return newList; //将新集合返回

}


02_集合框架(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);


03_集合框架(用LinkedList模拟栈数据结构的集合并测试)
* A:案例演示
* 需求:请用LinkedList模拟栈数据结构的集合,并测试
* 创建一个类将Linked中的方法封装

public class Stack {
private LinkedList list = new LinkedList(); //创建LinkedList对象

public void in(Object obj) {
list.addLast(obj); //封装addLast()方法
}

public Object out() {
return list.removeLast(); //封装removeLast()方法
}

public boolean isEmpty() {
return list.isEmpty(); //封装isEmpty()方法
}
}

04_集合框架(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);

}


05_集合框架(三种迭代的能否删除)

* 普通for循环,可以删除,但是索引要--
* 迭代器,可以删除,但是必须使用迭代器自身的remove方法,否则会出现并发修改异常
* 增强for循环不能删除


06_集合框架(静态导入的概述和使用)
* A:静态导入概述
* B:格式:
* import static 包名….类名.方法名;
* 可以直接导入到方法的级别
* C:注意事项
* 方法必须是静态的,如果有多个同名的静态方法,容易不知道使用谁?这个时候要使用,必须加前缀。由此可见,意义不大,所以一般不用,但是要能看懂。


07_集合框架(可变参数的概述和使用)
* A:可变参数概述
* 定义方法的时候不知道该定义多少个参数
* B:格式
* 修饰符 返回值类型 方法名(数据类型…  变量名){}
* C:注意事项:
* 这里的变量其实是一个数组
* 如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个

猜你喜欢

转载自blog.csdn.net/leeshuilian/article/details/80368358