ArrayList
使用ArrayList 删除重复值的方法
import java.util.ArrayList;
import java.util.Iterator;
@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<>(); //1,创建新集合
Iterator it = list.iterator(); //2,根据传入的集合(老集合)获取迭代器
while(it.hasNext()) { //3,遍历老集合
Object obj = it.next(); //记录住每一个元素
if(!newList.contains(obj)) { //如果新集合中不包含老集合中的元素
newList.add(obj); //将该元素添加
}
}
return newList;
}
}
重点注意一下
集合中
contains方法判断是否包含,底层依赖的是equals方法
remove方法判断是否删除,底层依赖的是equals方法
因此自定义类中要重写euqals方法 否则调用的是Object中的euqals (判断地址值是否想等)
否则上述两种功能会错误出现
import java.util.ArrayList;
import java.util.Iterator;
import com.heima.bean.Person;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo2_ArrayList {
/**
* * A:案例演示
* 需求:ArrayList去除集合中自定义对象元素的重复值(对象的成员变量值相同)
* B:注意事项
* 重写equals()方法的
contains方法判断是否包含,底层依赖的是equals方法
remove方法判断是否删除,底层依赖的是equals方法
*/
public static void main(String[] args) {
ArrayList list = new ArrayList(); //创建集合对象
list.add(new Person("张三", 23));
list.add(new Person("张三", 23));
list.add(new Person("李四", 24));
list.add(new Person("李四", 24));
list.add(new Person("李四", 24));
list.add(new Person("李四", 24));
//ArrayList newList = getSingle(list); //调用方法去除重复
//System.out.println(newList);
list.remove(new Person("张三", 23));
System.out.println(list);
}
/*
* 创建新集合将重复元素去掉
* 1,明确返回值类型,返回ArrayList
* 2,明确参数列表ArrayList
*
* 分析:
* 1,创建新集合
* 2,根据传入的集合(老集合)获取迭代器
* 3,遍历老集合
* 4,通过新集合判断是否包含老集合中的元素,如果包含就不添加,如果不包含就添加
*/
public static ArrayList getSingle(ArrayList list) {
ArrayList newList = new ArrayList<>(); //1,创建新集合
Iterator it = list.iterator(); //2,根据传入的集合(老集合)获取迭代器
while(it.hasNext()) { //3,遍历老集合
Object obj = it.next(); //记录住每一个元素
if(!newList.contains(obj)) { //如果新集合中不包含老集合中的元素
newList.add(obj); //将该元素添加
}
}
return newList;
}
}
================================================================================================
public class Person {
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
@Override
public boolean equals(Object obj) { //重写euqals方法 否则调用的是Object中的
Person p = (Person)obj; //由于父类无法调用 子类特有的成员变量和方法 所以需要向下转型
return this.name.equals(p.name) && this.age == p.age;
}
}
LinkedList
注意链表结构元素添加的顺序! 不要弄错了!
import java.util.LinkedList;
public class Demo3_LinkedList {
/**
* public void addFirst(E e)及addLast(E e)
* public E getFirst()及getLast()
* public E removeFirst()及public E removeLast()
* public E get(int index);
*/
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.addFirst("a");
list.addFirst("b");
list.addFirst("c");
list.addFirst("d");
list.addLast("e");
//System.out.println(list.getFirst());
//System.out.println(list.getLast());
//System.out.println(list.removeFirst());
//System.out.println(list.removeLast());
System.out.println(list.get(1));
System.out.println(list);
}
/*dcbae*/
}
利用LinkedList
模拟数据结构 队列 和栈
import java.util.LinkedList;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Main{
public static void main(String args[]) {
Stack s = new Stack();
s.in("a");
s.in("b");
s.in("c");
while(!s.isEmpty())
{
System.out.println(s.out()); //弹栈
}
Quene q = new Quene();
q.in("aaa");
q.in("bbb");
q.in("ccc");
while(!q.isEmpty())
{
System.out.println(q.out());
}
}
}
//栈结构 LinkedList(链表集合实现)
class Stack{
LinkedList link = new LinkedList();
//模拟 进栈
public void in(Object o) {
link.addLast(o);
}
//模拟 出栈
public Object out() {
return link.removeLast();
}
//模拟 栈结构是否为空
public boolean isEmpty(){
return link.isEmpty();
}
}
class Quene{
LinkedList link = new LinkedList();
//模拟 入队
public void in(Object o) {
link.addFirst(o);
}
//模拟 出队
public Object out() {
return link.removeLast();
}
//模拟 队列结构是否为空
public boolean isEmpty(){
return link.isEmpty();
}
}
泛型
泛型的一些小知识点
/**
* A:泛型概述
* B:泛型好处
* 提高安全性(将运行期的错误转换到编译期)
* 省去强转的麻烦
* C:泛型基本使用
* <>中放的必须是引用数据类型 !!!
* D:泛型使用注意事项
* 前后的泛型必须一致,或者后面的泛型可以省略不写(1.7的新特性菱形泛型)
*/
import java.util.ArrayList;
import java.util.Iterator;
import com.heima.bean.Person;
public class Demo1_Generic {
public static void main(String[] args) {
//demo1();
//int[] arr = new byte[5]; //数组要保证前后的数据类型一致
//ArrayList<Object> list = new ArrayList<Person>(); //集合的泛型要保证前后的数据类型一致
//ArrayList<Object> list = new ArrayList<>(); //1.7版本的新特性,菱形泛型
ArrayList<Object> list = new ArrayList<>(); //泛型最好不要定义成Object,没有意义
list.add("aaa");
list.add(true);
}
public static void demo1() {
ArrayList<Person> list = new ArrayList<Person>();
// list.add(110);
// list.add(true);
list.add(new Person("张三", 23));
list.add(new Person("李四", 24));
Iterator<Person> it = list.iterator();
while(it.hasNext()) {
//System.out.println(it.next());
//System.out.println(it.next().getName() + "..." + it.next().getAge());//next方法只能调用一次,如果调用多次会将指针向后移动多次
Person p = it.next();
System.out.println(p.getName() + "..." + p.getAge());
}
}
}
泛型遍历 以及 重复元素删除操作
import java.util.ArrayList;
import java.util.Iterator;
public class Main{
public static void main(String args[]) {
// ArrayList<String> list = new ArrayList<>();
// list.add("aaa");
// list.add("bbb");
// list.add("ccc");
// Iterator<String> it = list.iterator();
// while(it.hasNext()) {
// System.out.println(it.next());
// }
ArrayList<Student> list2 = new ArrayList<>();
list2.add(new Student("小明",18));
list2.add(new Student("小明",18));
list2.add(new Student("小明",18));
list2.add(new Student("小明",18));
list2.add(new Student("小红",20));
list2.add(new Student("小军",21));
Iterator<Student> it = list2.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
System.out.println("********");
//删除重复值以后
ArrayList<Student> newlist2 = getSingle(list2);
for(Student list : newlist2) {
System.out.println(list);
}
}
public static ArrayList<Student> getSingle(ArrayList<Student> list)
{
ArrayList<Student> newList = new ArrayList<>();
Iterator<Student> it = list.iterator();
while(it.hasNext()) {
Student s= it.next();
if(!newList.contains(s)) {
newList.add(s);
}
}
return newList;
}
}
class Student
{
private String name;
private int age;
public Student(String name,int age)
{
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "["+name+".."+age+"]";
}
@Override
public boolean equals(Object o) {
Student s = (Student)o;
return this.name.equals(s.name) && this.age == s.age;
}
}
泛型类以及泛型方法
public class Main<M> { //泛型类
/*public static void main(String[] args) {
}*/
private M m;
public Main(M m) {
super();
this.m = m;
}
public M getM() {
return m;
}
public void setM(M m) {
this.m = m;
}
public void show(String s) {
System.out.println(s);
}
public void show(int a) { //不影响基本数据类型
System.out.println(a);
}
public <T> void show(T t) { //泛型方法 (最好保证泛型方法和泛型类中的元素一致)
System.out.println(t); //如果泛型不一致 就要在方法上改变泛型
}
public static <Q> void print(Q q) { //静态方法加泛型 由于静态是由类的加载而加载的
System.out.println(); //所以在还没有创建对象之前就要声明泛型 而且尽量保证与类不一致
}
}
泛型接口
public class Demo4_Generic {
/**
* * A:泛型接口概述
* 把泛型定义在接口上
* B:定义格式
* public interface 接口名<泛型类型>
* C:案例演示
* 泛型接口的使用
*/
public static void main(String[] args) {
}
}
interface Inter<T> {
public void show(T t);
}
/*class Demo implements Inter<String> { //推荐用这种
@Override
public void show(String t) {
System.out.println(t);
}
}*/
class Demo<T> implements Inter<T> { //没有必要在实现接口的时候给自己类加泛型
@Override
public void show(T t) {
System.out.println(t);
}
}
泛型通配符
理解 泛型通配符以及 向下限定
import java.util.ArrayList;
import java.util.List;
import com.heima.bean.Person;
import com.heima.bean.Student;
public class Demo5_Generic {
/**
* * A:泛型通配符<?>
* 任意类型,如果没有明确,那么就是Object以及任意的Java类了
* B:? extends E
* 向下限定,E及其子类
* C:? super E
* 向上限定,E及其父类
*/
public static void main(String[] args) {
//List<?> list = new ArrayList<Integer>(); //当右边的泛型是不确定时,左边可以指定为?
ArrayList<Person> list1 = new ArrayList<>();
list1.add(new Person("张三", 23));
list1.add(new Person("李四", 24));
list1.add(new Person("王五", 25));
ArrayList<Student> list2 = new ArrayList<>();
list2.add(new Student("赵六", 26));
list2.add(new Student("周七", 27));
list1.addAll(list2);
System.out.println(list1);
}
}