Day16
集合框架
去除ArrayList集合中的重复值
package com. heima. list;
import java. util. ArrayList;
import java. util. Iterator;
@SuppressWarnings ( { "rawtypes" , "unchecked" } )
public class Demo1_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) ;
}
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;
}
}
去除ArrayList中重复自定义对象元素的重复值
package com. heima. list;
import java. util. ArrayList;
import java. util. Iterator;
import com. heima. bean. Person;
@SuppressWarnings ( { "rawtypes" , "unchecked" } )
public class Demo2_ArrayList {
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) ;
}
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;
}
}
LinkedList特有功能
package com. heima. list;
import java. util. LinkedList;
public class Demo3_LinkedList {
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. get ( 1 ) ) ;
System. out. println ( list) ;
}
}
栈和队列的数据结构
栈:先进后出
队列:先进先出
用LinkedList模拟栈的数据结构
package com. heima. list;
import java. util. LinkedList;
public class Stack {
private LinkedList list = new LinkedList ( ) ;
public void in ( Object obj) {
list. addLast ( obj) ;
}
public Object out ( ) {
return list. removeLast ( ) ;
}
public boolean isEmpty ( ) {
return list. isEmpty ( ) ;
}
}
package com. heima. list;
import java. util. LinkedList;
public class Demo4_LinkedList {
public static void main ( String[ ] args) {
Stack s = new Stack ( ) ;
s. in ( "a" ) ;
s. in ( "b" ) ;
s. in ( "c" ) ;
s. in ( "d" ) ;
while ( ! s. isEmpty ( ) ) {
System. out. println ( s. out ( ) ) ;
}
}
public static void demo1 ( ) {
LinkedList list = new LinkedList ( ) ;
list. addLast ( "a" ) ;
list. addLast ( "b" ) ;
list. addLast ( "c" ) ;
list. addLast ( "d" ) ;
while ( ! list. isEmpty ( ) ) {
System. out. println ( list. removeLast ( ) ) ;
}
}
}
泛型(generic)概述和基本使用
package com. heima. generic;
import java. util. ArrayList;
import java. util. Iterator;
import com. heima. bean. Person;
public class Demo1_Generic {
public static void main ( String[ ] args) {
ArrayList< Object> list = new ArrayList < > ( ) ;
list. add ( "aaa" ) ;
list. add ( true ) ;
}
public static void demo1 ( ) {
ArrayList< Person> list = new ArrayList < Person> ( ) ;
list. add ( new Person ( "张三" , 23 ) ) ;
list. add ( new Person ( "李四" , 24 ) ) ;
Iterator< Person> it = list. iterator ( ) ;
while ( it. hasNext ( ) ) {
Person p = it. next ( ) ;
System. out. println ( p. getName ( ) + "..." + p. getAge ( ) ) ;
}
}
}
ArrayList存储字符串和自定义对象并遍历泛型版
package com. heima. generic;
import java. util. ArrayList;
import java. util. Iterator;
import com. heima. bean. Person;
public class Demo2_Generic {
public static void main ( String[ ] args) {
ArrayList< Person> list = new ArrayList < > ( ) ;
list. add ( new Person ( "张三" , 23 ) ) ;
list. add ( new Person ( "李四" , 24 ) ) ;
list. add ( new Person ( "王五" , 25 ) ) ;
list. add ( new Person ( "赵六" , 26 ) ) ;
Iterator< Person> it = list. iterator ( ) ;
while ( it. hasNext ( ) ) {
Person p = it. next ( ) ;
System. out. println ( p. getName ( ) + "..." + p. getAge ( ) ) ;
}
}
public static void demo1 ( ) {
ArrayList< String> list = new ArrayList < > ( ) ;
list. add ( "a" ) ;
list. add ( "b" ) ;
list. add ( "c" ) ;
list. add ( "d" ) ;
Iterator< String> it = list. iterator ( ) ;
while ( it. hasNext ( ) ) {
System. out. println ( it. next ( ) ) ;
}
}
}
泛型的由来
泛型类的概述及其使用
A:泛型类概述(T)
*把泛型定义在类上;
B:定义格式:
*public class 类名<泛型类型1,>
C:注意事项
*泛型类必须是引用类型
泛型方法的概述和使用
A:泛型方法概述
*把泛型定义在方法上;
B:定义格式:
*public <泛型类型> 返回类型 方法名(泛型类型 变量名)
C:注意事项
* 静态方法必须声明自己的泛型
*方法泛型声明与类泛型声明最好一致;
*若方法需要自己的泛型,单独声明
泛型接口的概述和使用
A: 泛型接口概述
* 把泛型定义在接口上;
B: 定义格式:
* public interface 接口名( 泛型类型)
C: 案例演示
package com. heima. generic;
public class Demo4_Generic {
public static void main ( String[ ] args) {
}
}
interface Inter < T> {
public void show ( T t) ;
}
class Demo < T> implements Inter < T> {
@Override
public void show ( T t) {
System. out. println ( t) ;
}
}
泛型通配符
package com. heima. generic;
import java. util. ArrayList;
import java. util. List;
import com. heima. bean. Person;
import com. heima. bean. Student;
public class Demo5_Generic {
public static void main ( String[ ] args) {
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) ;
}
}
增强for循环
public static void demo1 ( ) {
int [ ] arr = { 11 , 22 , 33 , 44 , 55 } ;
for ( int i : arr) {
System. out. println ( i) ;
}
ArrayList< String> list = new ArrayList < > ( ) ;
list. add ( "a" ) ;
list. add ( "b" ) ;
list. add ( "c" ) ;
list. add ( "d" ) ;
for ( String string : list) {
System. out. println ( string) ;
}
ArrayList存储自定义数组,用增强for循环
public static void demo2 ( ) {
ArrayList< Person> list = new ArrayList < > ( ) ;
list. add ( new Person ( "张三" , 23 ) ) ;
list. add ( new Person ( "李四" , 24 ) ) ;
list. add ( new Person ( "王五" , 25 ) ) ;
list. add ( new Person ( "赵六" , 26 ) ) ;
for ( Person person : list) {
System. out. println ( person) ;
}
}
三种迭代的能否删除
* 普通for 循环, 可以删除, 但是如果想都删掉, 索引要--
* 借助get方法和remove方法
for ( int i = 0 ; i < list. size ( ) ; i++ ) {
if ( "b" . equals ( list. get ( i) ) ) {
list. remove ( i-- ) ;
}
}
* 迭代器 : 可以删除, 但是必须使用迭代器自身的remove方法, 否则就会出现并发修改异常
* 借助hasnext ( ) 方法和next ( ) 方法;
Iterator< String> it = list. iterator ( ) ;
while ( it. hasNext ( ) ) {
if ( "b" . equals ( it. next ( ) ) ) {
it. remove ( ) ;
}
}
for ( Iterator< String> it2 = list. iterator ( ) ; it2. hasNext ( ) ; ) {
if ( "b" . equals ( it2. next ( ) ) ) {
it2. remove ( ) ;
}
}
* 增强for 循环不能删除, 只能遍历, 删除会出现并发修改异常
* 底层借助的是迭代器
for ( String string : list) {
if ( "b" . equals ( string) ) {
list. remove ( "b" ) ;
}
}
静态导入的概述和使用
* 格式:
* import static 包名. . . 类名. 方法名
* 可以直接导入方法的级别
C: 注意事项
* 方法必须是静态的, 如果有多个同名的静态方法, 容易不知道使用谁? 这个时候要使用, 必须加前缀. 由此可见, 意义不大, 所以一般不用, 但需看得懂
package com. heima. jdk5;
import static java. util. Arrays. sort;
import java. util. Arrays;
public class Demo2_StaticImport {
public static void main ( String[ ] args) {
int [ ] arr = { 55 , 22 , 33 , 44 , 11 } ;
sort ( arr) ;
System. out. println ( Arrays. toString ( arr) ) ;
}
}
可变参数的概述和使用
package com. heima. jdk5;
public class Demo3_ChangeableArgs {
public static void main ( String[ ] args) {
print ( 11 , 22 , 33 , 44 , 55 ) ;
System. out. println ( "---------------" ) ;
print ( ) ;
}
public static void print ( int . . . arr) {
for ( int i = 0 ; i < arr. length; i++ ) {
System. out. println ( arr[ i] ) ;
}
}
}
Arrays工具类的asList( )方法的使用
* 数组转换成集合
* 数组转换成集合虽然不能增加或减少元素, 但是可以用集合的思想操作数组, 也就是说可以使用其他集合中的方法
public static void demo1 ( ) {
String[ ] arr = { "a" , "b" , "c" } ;
List< String> list = Arrays. asList ( arr) ;
System. out. println ( list) ;
public static void demo2 ( ) {
int [ ] arr = { 11 , 22 , 33 , 44 , 55 } ;
List< int [ ] > list = Arrays. asList ( arr) ;
System. out. println ( list) ;
Integer[ ] arrIntegers = { 11 , 22 , 33 , 44 , 55 } ;
List< Integer> list2 = Arrays. asList ( arrIntegers) ;
System. out. println ( list2) ;
}
* 集合转数组
ArrayList< String> list = new ArrayList < > ( ) ;
list. add ( "a" ) ;
list. add ( "b" ) ;
list. add ( "c" ) ;
list. add ( "d" ) ;
String[ ] arr = list. toArray ( new String [ 10 ] ) ;
for ( String string : arr) {
System. out. println ( string) ;
}
}
集合ArrayList嵌套ArrayList
package com. heima. list;
import java. util. ArrayList;
import com. heima. bean. Person;
public class Demo6_ArrayList2 {
public static void main ( String[ ] args) {
ArrayList< ArrayList< Person> > list = new ArrayList < > ( ) ;
ArrayList< Person> first = new ArrayList < > ( ) ;
first. add ( new Person ( "张三" , 23 ) ) ;
first. add ( new Person ( "李四" , 24 ) ) ;
first. add ( new Person ( "王五" , 25 ) ) ;
ArrayList< Person> second = new ArrayList < > ( ) ;
second. add ( new Person ( "赵六" , 26 ) ) ;
second. add ( new Person ( "周七" , 27 ) ) ;
second. add ( new Person ( "孙八" , 28 ) ) ;
list. add ( first) ;
list. add ( second) ;
for ( ArrayList< Person> a : list) {
for ( Person p : a) {
System. out. println ( p) ;
}
}
}
}