一、 基础回顾
1 集合
1.1 集合的类型与各自的特性
---|Collection: 单列集合 ---|List: 有存储顺序, 可重复 ---|ArrayList: 数组实现, 查找快, 增删慢 由于是数组实现, 在增和删的时候会牵扯到数组 增容, 以及拷贝元素. 所以慢。数组是可以直接按索引查找, 所以查找时较快 ---|LinkedList: 链表实现, 增删快, 查找慢由于链表实现, 增加时只要让前一个元素记住自己就可以, 删除时让前一个元素记住后一个元素, 后一个元素记住前一个元素. 这样的增删效率较高但查询时需要一个一个的遍历, 所以效率较低 ---|Vector: 和ArrayList原理相同, 但线程安全, 效率略低 和ArrayList实现方式相同, 但考虑了线程安全问题, 所以效率略低 ---|Set: 无存储顺序, 不可重复 ---|HashSet 线程不安全,存取速度快。底层是以哈希表实现的。 ---|TreeSet 红-黑树的数据结构,默认对元素进行自然排 序(String)。如果在比较的时候两个对象 返回值为0,那么元素重复。 ---| Map: 键值对 键不可重复,键可以重复 ---|HashMap 线程不安全,存取速度快。底层是以哈希表实现的. ---|TreeMap 红-黑树的数据结构,默认对元素进行自然排 序(String)。如果在比较的时候两个对象 返回值为0,那么元素重复 ---|HashTable 底层也是使用了哈希表 维护的,存取的读取快,存储元素是 无序的。 |
1.2 遍历集合
1.2.1 遍历集合的几种方式
1, 使用迭代器Iterator的方式。
2, 使用增强for循环的方式。
3, 如果有下标,则可以使用下标的方式。
1.2.2 遍历数组
public static void main(String[] args) { // 遍历数组: String[] arr = new String[] { "xx", "yy", "zz" }; // 1,增强的for循环 for (String elt : arr) { System.out.println(elt); } // 2,下标的方式 for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } }
1.2.3 遍历List
public static void main(String[] args) {
// 遍历List:
List<String> list = new ArrayList<String>();
list.add("aa");
list.add("bb");
list.add("cc");
// 1,增强的for循环
for (String elt : list) {
System.out.println(elt);
}
// 2,下标
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
// 3,迭代器
for (Iterator<String> iter = list.iterator(); iter.hasNext();) {
String elt = iter.next();
System.out.println(elt);
}
}
1.2.4 遍历Set
public static void main(String[] args) {
// 遍历Set:
Set<String> set = new HashSet<String>();
set.add("dd");
set.add("ee");
set.add("ff");
// 1,增强的for循环
for (String elt : set) {
System.out.println(elt);
}
// 2,迭代器
for(Iterator<String> iter = set.iterator(); iter.hasNext() ; ){
String elt = iter.next();
System.out.println(elt);
}
}
1.2.5 遍历Map
public static void main(String[] args) {
// 遍历Map:
Map<String, String> map = new HashMap<String, String>();
map.put("aa", "xx");
map.put("bb", "yy");
map.put("cc", "zz");
// 1,增强的for循环(Entry集合)
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry);
}
// 2,增强的for循环(Key集合)
for(String key : map.keySet()){
System.out.println(key + " = " + map.get(key));
}
// 3,遍历值的集合
for(String value : map.values()){
System.out.println(value);
}
}
2 泛型(Generic)
当集合中存储的对象类型不同时,那么会导致程序在运行的时候的转型异常
import java.util.ArrayList;
import java.util.Iterator;
public class Demo5 {
public static void main(String[] args) {
ArrayList arr = new ArrayList();
arr.add(new Tiger("华南虎"));
arr.add(new Tiger("东北虎"));
arr.add(new Sheep("喜羊羊"));
System.out.println(arr);
Iterator it = arr.iterator();
while (it.hasNext()) {
Object next = it.next();
Tiger t = (Tiger) next;
t.eat();
}
}
}
class Tiger {
String name;
public Tiger() {
}
public Tiger(String name) {
this.name = name;
}
@Override
public String toString() {
return "Tiger@name:" + this.name;
}
public void eat() {
System.out.println(this.name + "吃羊");
}
}
class Sheep {
String name;
public Sheep() {
}
public Sheep(String name) {
this.name = name;
}
@Override
public String toString() {
return "Sheep@name:" + this.name;
}
public void eat() {
System.out.println(this.name + "吃青草");
}
}
原因 : 发现虽然集合可以存储任意对象 , 但是如果需要使用对象的特有方法 , 那么就需要类型转换 , 如果集合中存入的对象不同 , 可能引发类型转换异常 .
[Tiger@name:华南虎, Tiger@name:东北虎, Sheep@name:喜羊羊] 华南虎吃羊 东北虎吃羊 Exception in thread "main" java.lang.ClassCastException: cn.itcast.gz.map.Sheep cannot be cast to cn.itcast.gz.map.Tiger at cn.itcast.gz.map.Demo5.main(Demo5.java:17) |
出现问题:
存入的是特定的对象,取出的时候是Object对象,需要强制类型转换,可能诱发类型转换异常.
无法控制存入的是什么类型的对象,取出对象的时候进行强转时可能诱发异常.而且在编译时期无法发现问题.
虽然可以再类型转换的时候通过if语句进行类型检查(instanceof),但是效率较低.(例如吃饭的时候,还需要判断米饭里有没有沙子,吃饭效率低).可以通过给容器加限定的形式规定容器只能存储一种类型的对象.
就像给容器贴标签说明该容器中只能存储什么样类型的对象。
所以在jdk5.0后出现了泛型
泛型应用:
格式
1. 集合类<类类型> 变量名 = new 集合类<类类型>();
public class Demo5 {
public static void main(String[] args) {
// 使用泛型后,规定该集合只能放羊,老虎就进不来了.
ArrayList<Sheep> arr = new ArrayList<Sheep>();
arr.add(new Sheep("美羊羊"));
arr.add(new Sheep("懒洋洋"));
arr.add(new Sheep("喜羊羊"));
// 编译失败
// arr.add(new Tiger("东北虎"));
System.out.println(arr);
Iterator<Sheep> it = arr.iterator();
while (it.hasNext()) {
// 使用泛型后,不需要强制类型转换了
Sheep next = it.next();
next.eat();
}
}
}
1. 将运行时的异常提前至编译时发生。
2. 获取元素的时候无需强转类型,就避免了类型转换的异常问题
格式 通过<> 来指定容器中元素的类型.
什么时候使用泛型:当类中操作的引用数据类型不确定的时候,就可以使用泛型类.
JDK5.0之前的Comparable
package java.lang; public interface Comparable { public int compareTo(Object o); } |
JDK5.0之后的Comparable
package java.lang; public interface Comparable<T> { public int compareTo(T o); } |
这里的<T>表示泛型类型,随后可以传入具体的类型来替换它.
细节一
声明好泛型类型之后,集合中只能存放特定类型元素
public class Demo6 {
public static void main(String[] args) {
//创建一个存储字符串的list
ArrayList<String> arr=new ArrayList<String>();
arr.add("gz");
arr.add("itcast");
//存储非字符串编译报错.
arr.add(1);
}
}
泛型类型必须是引用类型细节二:
public class Demo6 {
public static void main(String[] args) {
// 泛型类型必须是引用类型,也就是说集合不能存储基本数据类型
// ArrayList<int> arr2=new ArrayList<int>();
// 使用基本数据类型的包装类
ArrayList<Integer> arr2 = new ArrayList<Integer>();
}
}
细节三: 使用泛型后取出元素不需要类型转换.
public class Demo6 {
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<String>();
arr.add("gzitcast");
arr.add("cditcast");
arr.add("bjitcast");
//使用泛型后取出元素不需要类型转换.
String str=arr.get(0);
System.out.println();
}
}
1.1. 泛型方法
需求:写一个函数,调用者传递什么类型的变量,该函数就返回什么类型的变量?
实现一:
由于无法确定具体传递什么类型的数据.那么方法的形参就定义为Object类型.返回值也就是Object类型.但是使用该函数时需要强制类型转换.
private Object getDate(Object obj) { return obj; } |
当不进行强制类型转换能否写出该功能.?
目前所学的知识无法解决该问题
就需要使用泛型类解决
使用的泛型的自定义来解决以上问题。
泛型: 就是将类型当作变量处理。规范泛型的定义一般是一个大写的任意字母。
1. 函数上的泛型定义 当函数中使用了一个不明确的数据类型,那么在函数上就可以进行泛型的定义。 public <泛型的声明> 返回值类型 函数名( 泛型 变量名 ){ } |
public static void main(String[] args) {
} public <T> T getData(T data) { return data; } |
细节:
使用泛型方法前需要进行泛型声明,使用一对尖括号 <泛型>,声明的位置在static后返回值类型前。
当一个类中有多个函数声明了泛型,那么该泛型的声明可以声明在类上。
1.2. 泛型类
格式
2. 类上的泛型声明 修饰符 class 类名<泛型>{ } |
public class Demo6<T> {
public static void main(String[] args) {
// 使用泛型类,创建对象的时候需要指定具体的类型
new Demo6<Integer>().getData(5);
}
public T getData(T data) {
return data;
}
// 反序任意类型数组
public void reverse(T[] arr) {
int start = 0;
int end = arr.length - 1;
for (int i = 0; i < arr.length; i++) {
if (start < end) {
T temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}
}
在泛型类中定义一个静态方法
public class Demo6<T> {
} |
注意:静态方法不可以使用类中定义的泛型
因为类中的泛型需要在对象初始化时指定具体的类型,而静态优先于对象存在。那么类中的静态方法就需要单独进行泛型声明,声明泛型一定要写在static后,返回值类型之前
泛型类细节:
1、创建对象的时候要指定泛型的具体类型 2、创建对象时可以不指定泛型的具体类型(和创建集合对象一眼)。默认是Object,例如我们使用集合存储元素的时候没有使用泛型就是那么参数的类型就是Object 3、类上面声明的泛型只能应用于非静态成员函数,如果静态函数需要使用泛型,那么 需要在函数上独立声明。 4、如果建立对象后指定了泛型的具体类型,那么该对象操作方法时,这些方法只能操作一种数据类型。 5、所以既可以在类上的泛型声明,也可以在同时在该类的方法中声明泛型。 |
泛型练习:
定义泛型成员
|
|
1.3. 泛型接口
public class Demo8 {
public static void main(String[] args) {
MyInter<String> my = new MyInter<String>();
my.print("泛型");
MyInter2 my2 = new MyInter2();
my.print("只能传字符串");
}
}
interface Inter<T> {
void print(T t);
}
// 实现不知为何类型时可以这样定义
class MyInter<T> implements Inter<T> {
public void print(T t) {
System.out.println("myprint:" + t);
}
}
//使用接口时明确具体类型。
class MyInter2 implements Inter<String> {
@Override
public void print(String t) {
System.out.println("myprint:" + t);
}
}
3 IO 流
3.1 IO流的分类
| 输入流 | 输出流 | 说明 |
字节流 | InputStream | OutputStream | 字节流是处理字节的(二进制) |
字符流 | Reader | Writer | 字符流是处理字符的 |
注:这几个类都是抽象类。
3.2 读文件的代码
public static void main(String[] args) {
String path = "c:/a.txt";
FileInputStream in = null;
try {
// 打开流
in = new FileInputStream(path);
// 使用流读文件内容
int b = in.read();
while (b != -1) {
System.out.print((char) b);
b = in.read();
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 释放资源
if (in != null) {
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
3.3 拷贝文件的代码
public static void main(String[] args) {
String srcPath = "c:/a.txt";
String destPath = "c:/b.txt";
// 一定要使用字节流
InputStream in = null;
OutputStream out = null;
try {
// 打开流
in = new FileInputStream(srcPath);
out = new FileOutputStream(destPath);
// 使用流
byte[] buf = new byte[1024 * 8];
for (int len = -1; (len = in.read(buf)) != -1;) {
out.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
public static void main(String[] args) {
String srcPath = "c:/a.txt";
String destPath = "c:/b.txt";
// 一定要使用字节流
InputStream in = null;
OutputStream out = null;
try {
// 打开流
in = new FileInputStream(srcPath);
out = new FileOutputStream(destPath);
// 使用流
byte[] buf = new byte[1024 * 8];
for (int len = -1; (len = in.read(buf)) != -1;) {
out.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
4 多线程
4.1 启动线程方式
1, 自定义的类继承Thread类。
使用代码为new MyThread().start()
2,自定义的类实现Runnable接口。
使用代码为new Thread(new MyRunnable()).start
4.2 代码
以下代码是分别用两种方式启动线程(还是用到了匿名内部类)
private static int count = 100;
public static void main(String[] args) {
// 用继承Thread类的方式启动一个线程
new Thread() {
public void run() {
synchronized (StartThreadTest.class) {
while (count > 0) {
count--;
System.out.println(Thread.currentThread() + "卖了一张票,还剩" + count);
}
}
}
}.start();
// 用实现Runnable接口的方式启动一个线程
new Thread(new Runnable() {
public void run() {
synchronized (StartThreadTest.class) {
while (count > 0) {
count--;
System.out.println(Thread.currentThread() + "卖了一张票,还剩" + count);
}
}
}
}).start();
}
二、 Junit单元测试
1.1. Junit单元测试框架的基本使用
一、搭建环境:
导入junit.jar包(junit4)
二、写测试类:
0,一般一个类对应一个测试类。
1,测试类与被测试类最好是放到同一个包中(可以是不同的源文件夹)
2,测试类的名字为被测试类的名字加Test后缀。
三:写测试方法:
0,一般一个方法对应一个单元测试方法。
1,测试方法的名字为test前缀加被测试方法的名字,如testAddPerson()。
2,单元测试方法上面要加上@Test注解(org.junit.Test)!
3,单元测试方法不能有参数,也不能有返回值(返回void)!测试的方法不能是静态的方法。
四、测试方法的基本使用:
1,可以单独执行一个测试方法,也可以一次执行所有的、一个包的、一个类中所有的测试方法。
2,执行完后,显示绿色表示测试成功;显示红色表示测试失败(抛异常后会测试失败)。
1.2. Assert断言工具类
其中有一些静态的工具方法(不符合期望就抛异常):
assertTrue(...) 参数的值应是true
assertFalse(...) 参数的值应是false
assertNull(...) 应是null值
assertNotNull(...) 应是非null的值
assertSame(...) 使用==比较的结果为true(表示同一个对象)
AssertNotSame(...) 使用==比较的结果为false
assertEquals(...) 两个对象equals()方法比较结果为true
1.3. 用于准备环境、清理环境的方法
@Test
表示单元测试方法。
@Before
所修饰的方法应是非static的(且没有参数,返回值为void)。
表示这个方法会在本类中的每个单元测试方法之前都执行一次。
@After
所修饰的方法应是非static的(且没有参数,返回值为void)。
表示这个方法会在本类中的每个单元测试方法之后都执行一次。
@BeforeClass
所修饰的方法应是static的(且没有参数,返回值为void)。
表示这个方法会在本类中的所有单元测试方法之前执行,只执行一次。
@AfterClass
所修饰的方法应是static的(且没有参数,返回值为void)。
表示这个方法会在本类中的所有单元测试方法之后执行,只执行一次。
三、 内省(Introspector)
1 为什么要学内省?
开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都使用反射技术完成此类操作过于麻烦,所以sun公司开发了一套API,专门用于操作java对象的属性。
内省是用于操作java对象的属性的,那么以下问题我们必须要清楚。
问题一: 什么是Java对象的属性和属性的读写方法?
问题二: 如何通过内省访问到javaBean的属性 ?
1. 通过PropertyDescriptor类操作Bean的属性.
public static void testPropertyDescriptor() throws Exception{
Person p = new Person();
PropertyDescriptor propertyDescriptor = new PropertyDescriptor("id",Person.class);
//获取属性的写的方法。
Method writeMethod = propertyDescriptor.getWriteMethod();
Method readMethod = propertyDescriptor.getReadMethod();
propertyDescriptor.getReadMethod();
writeMethod.invoke(p, 12);
System.out.println(readMethod.invoke(p, null));
}
2. 通过Introspector类获得Bean对象的 BeanInfo,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后通过反射机制来调用这些方法。
public static void testIntrospector() throws Exception{
BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
PropertyDescriptor[] descriptor = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor itemProperty : descriptor){
System.out.println(itemProperty.getReadMethod().getName());
}
}
存在的问题: sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单、易用的API操作Bean的属性——BeanUtils。
public static void main(String[] args) throws Exception {
Person p = new Person();
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
try {
if(value!=null){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MM dd");
Date d = dateFormat.parse((String) value);
return d;
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}, Date.class);
BeanUtils.setProperty(p,"id","110");
BeanUtils.setProperty(p,"name","狗娃");
BeanUtils.setProperty(p, "birthDay","1992 12 12");
System.out.println(p.getId() +"=="+ p.getName()+"======"+p.getBirthDay());
}
四、 Properties类与配置文件
4.3 Properties配置文件说明
Properties类对应.properties文件。文件内容是键值对,键值对之间使用"="或空格隔开。开头是"#"的表示注释
Properties类在加载.properties文件时使用的iso8859-1的编码。所以这个文件中的中文要特殊处理:如果这个配置文件中有中文就必须要进行转义,使用native2ascii.exe命令操作:
native2ascii d:/my.properties d:/my2.properties
使用Properties类中的load(InputStream) 方法可以加载配置文件,使用其中的store(OutputStream) 方法可以保存配置到指定文件。
更多的信息可以看Properties类的API文档。
4.4 加载配置文件
public static void testLoadProperties() throws Exception {
Properties properties = new Properties();
InputStream in = new FileInputStream("E:/itcast/config.properties");
properties.load(in); // 加载
in.close();
System.out.println(properties);
}
4.5 写配置文件
public static void testStoreProperties() throws Exception {
// 准备配置信息
Properties properties = new Properties();
properties.setProperty("name", "李四");
properties.setProperty("age", "20");
// 准备
OutputStream out = new FileOutputStream("d:/my.properties");
String comments = "这是我的配置文件";
// 写出去
properties.store(out, comments);
out.close();
}
作业:使用properties读取配置文件,读取数据库的用户名、密码。并且打包成jar包。
4.6 使用Properties类
public class DBUtil {
static Properties properties = new Properties();
static{
try {
Class clazz = DBUtil.class;
InputStreamReader fileReader =
new InputStreamReader(clazz.getResourceAsStream("/db.properties"));
properties.load(fileReader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getUserName(){
String userName =properties.getProperty("userName");
return userName;
}
public static String getPassword(){
return properties.getProperty("password");
}
public static void main(String[] args) {
System.out.println("用户名:"+ getUserName());
System.out.println("密码: "+ getPassword());
}
}
五、 文件路径
1.1. 绝对路径
以根目录或某盘符开头的路径(或者说完整的路径)
例如:
l c:/a.txt (Windows操作系统中)
l c:/xxx/a.txt (Windows操作系统中)
l /var/xx/aa.txt (Linux操作系统中)
绝对路径的问题: 比如C:\abc\a.properties文件路径,该路径在windows上执行没有 问题,但是如果把该项目移动到linux上面执行 ,该路径就会出现问题了,因为在linux上面没有c盘的,只有根目录\。
1.2. 相对路径
相对于当前路径的一个路径。例如当前文件夹为c:/abc时:相对路径a.txt表示c:/abc/a.txt,相对路径xx/a.txt = c:/abc/xx/a.txt
l . 表示当前文件夹
l .. 表示上级文件夹
相对路径存在的问题:相对路径是相对于目前执行class文件的时候,控制台所在的路径,这样子也会导致出现问题。
1.3. Java程序中的相对路径
public class PathTest {
public static void main(String[] args) throws Exception {
System.out.println(new File("a.txt").getAbsolutePath());
}
}
测试代码:
在命令行中使用cd命令切换到不同的路径下试试,可以看到以上所说的效果。
在Eclipse中,当前路径是工程的根目录。
1.4. classpath路径
1.4.1. classpath路径说明
在Java程序中,一般情况下使用绝对路径还是相对路径都不太合适,因为Java程序的jar包所放的位置不确定,执行java程序时当前的路径也不确定,所以不合适。一般在Java程序中我们会把资源放到classpath中,然后使用classpath路径查找资源。
Classpath路径:就是使用classpath目前的路径。
1.4.2. 获取classpath中的资源(InputStream)
public static void main(String[] args) throws Exception {
Class clazz = new ClassPathTest().getClass();
// 开头的'/'表示classpath的根目录,这个是表示从classpath的根目录中开始查找资源
InputStream in = clazz.getResourceAsStream("/cn/itcast/my.properties");
// 如果开头没有'/',表示从当前这个class所在的包中开始查找
InputStream in2 = clazz.getResourceAsStream("my.properties");
}