1.集合
a.为什么出现集合类?
面向对象语言对事物的体现都是以对象的形式,所以为了方便的多个对象操作,就对对象进行存储,
集合就是对象最常用的一种方式
b.数组和集合类都是容器,有何不同?
数组虽然也可以放存储对象,但长度是固定的,集合长度是可变的,数组中可以存储基本数据类型,集合只能存储对象。
c.集合类的特点
集合只用于存储对象,集合长度是可变的,集合可以存储不同类型的对象
d.ArrayList和LinkedList的区别:
1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。
3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。
2.泛型
构造方法中使用泛型,多个泛型,通配符,泛型方法,泛型数组
public class Person<E,V> {
private E pid;
private String pname;
private V money[];
public V[] getMoney() {
return money;
}
public void setMoney(V[] money) {
this.money = money;
}
public E getPid() {
return pid;
}
public void setPid(E pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
}
public class TestFan {
public static void main(String[] args) {
Person<String, ?> person=new Person<String, Integer>();
}
}
3.List
a.ArrayList:数组结构,查询快,增删改慢
b.LinkedList(特有方法):链表结构,查询慢,增删改快
c.Vector:增删改查慢
package com.zking.test;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class TestList {
public static void main(String[] args) {
// List Set Map
//List
//重复?可以重复
//有序?有序
// List<String> list=new ArrayList<String>();
// list.add("A");
// list.add("B");
// list.add("C");
// list.add("A");
// System.out.println(list.size());
//
// for (String string : list) {
// System.out.println(string);
// }
LinkedList<String> list=new LinkedList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("A");
list.addFirst("头");
list.addLast("尾");
list.removeFirst();
for (String string : list) {
System.out.println(string);
}
}
}
4.Set
package com.zking.test;
import java.util.HashSet;
import java.util.TreeSet;
public class TestSet {
public static void main(String[] args) {
//Set
//重复?不能添加重复
//有序?无序(放进去的顺序和取出来的顺序不一样)
HashSet<String> hashSet=new HashSet<String>();
hashSet.add("A");
hashSet.add("C");
hashSet.add("B");
hashSet.add(null);
//HashSet可以加空值
for (String string : hashSet) {
System.out.println(string);
}
//TreeSet不可以加空值
// TreeSet<String> treeSet=new TreeSet<String>();
// treeSet.add("A");
// treeSet.add("C");
// treeSet.add("B");
// treeSet.add(null);
// for (String string : treeSet) {
// System.out.println(string);
// }
}
}
5.Map(重点)
a.HashMap 可以加空值(无序)
b.HashTable 不能加空值(无序)
c.TreeMap 可以加空值(有序)
d.迭代Map(重点)
package com.zking.test;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class TestMap {
//键值对
public static void main(String[] args) {
Map<String, Object> map=new HashMap<String, Object>();
map.put("A", "AAAA");
map.put("2", "2222");
map.put("3", "3333");
map.put("王", "大小王");
//遍历
//01.获取所有的键,再根据键获取值
// Set<String> strings=map.keySet();
// for (String string : strings) {
// System.out.println(string+":"+map.get(string));
// }
//02.获取所有的值
// Collection<Object> collection=map.values();
// for (Object object : collection) {
// System.out.println(object);
// }
//03.获取键和值(一个一个)
Set<Entry<String, Object>> entries=map.entrySet();
for (Entry<String, Object> entry : entries) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
6.Properties配置
例子:利用Properties修改DBHelper读取
a.首先在项目的 WebRoot-->WEB-INF-->lib路径里面导入(sqljdbc4.jar)数据库的jar包。
b.在src目录下面写DBHelper类
package com.zking.util;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
public class DBHelper {
//SQLServerDriver
public static void main(String[] args) {
Connection conn=getConn();
System.out.println(conn);
System.out.println(DRIVERCLASS);
close(conn, null, null);
}
private static String DRIVERCLASS;
private static String URL;
private static String UNAME;
private static String UPWD;
static{
try {
FileInputStream fis=new FileInputStream("db.Properties");
Properties pro=new Properties();
pro.load(fis);
DRIVERCLASS=pro.getProperty("DRIVERCLASS");
URL=pro.getProperty("URL");
UNAME=pro.getProperty("UNAME");
UPWD=pro.getProperty("UPWD");
Class.forName(DRIVERCLASS);
} catch (Exception e) {
// TODO: handle exception
}
}
public static Connection getConn(){
try {
return DriverManager.getConnection(URL, UNAME, UPWD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void close(Connection conn,PreparedStatement ps,ResultSet rs){
try {
if(conn!=null && !conn.isClosed()){
conn.close();
}
if(ps!=null){
ps.close();
}
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
c.在项目下面WebRoot目录同级新建db.properties文件
DRIVERCLASS=com.microsoft.sqlserver.jdbc.SQLServerDriver
URL=jdbc:sqlserver://localhost:1433;databaseNAME=master
UNAME=sa
UPWD=123
欢迎大家来指出我的不足O(∩_∩)O