2017年6月28日 上午8:05
1.java.util.
collections:算法集合
collection
List set
arraylist Linkedlish hashset treeSet
collection 不唯一 无序
List 不唯一 有序
Set 唯一 无序
Map :键值对
Arraylist : 长度可变的数组
* list.size()
* list.get(index) 返回类型是Object (一般需要强转)
* list.add(“AA”);
* list.add(2,”BB”);
* 当index小于size()时,会后移,不会覆盖
* 当大于时,数组越界
* list.add(Object)
* list.addAll(collection)
* 可重复
* for(Object ob : list){}这里必须为Object,不能写具体的对象类
* Object o = list.remove(index) 删除执行位置,返回这个对象
* Boolean ans = list.remove(Object)删除指定第一个对象 返回true false
* remove()的两个方法容易混
* remove()的删除时,注意:删一个移动一次,后面所有元素下标都会改变,容易越界错误,或者元素位置判断错误
* list.contains(Object) 返回true false
* list可以添加null元素
* list.clear()移除所有
* list.indexOf(Object)
* list.empty()
* list.toArray();
* 首次,默认长度为10,以后没有增加原先的一半
* list没有insert()方法
* 改变list中的内容
1 2 3
| list.remove(i); list.add(i,strings.toString());
|
LinkedList : 链表的存储方式
* list.addFirst()
* list.addLast()
* list.removeLast()
* list.removeFirst()
* List list = new LinkedList();
* 这个list对象不可以调.addFirst()方法,这是子类的特有方法
* list.getFirst()
* list.getLast()
set
* Set set = new HashSet()
* set.add(“aa”);
* set.remove(“aa”);
* 当再插入”aa”时,不会报错,但是只有一个“aa”
* set没有get方法,因为是无序的
map
* Map map = new HashMap();
* map.put(“key”,”value”);
* map.put(null,null)正确
* map.put(“key1”,”value1”)
* map.put(“key1”,”value2”)
* 这时 value1 就被覆盖了
* map.put(“key”,Object)
* map.get(“key”); 返回Object
* map.size();
* map.keySet();key集合 返回set
* map.Values();value集合 返回collection
* 如果value是Object,那么会执行toString()方法
* map.containsKey(“key”);是否包含 true false
* map.remove(“key”);
* **map不可以直接遍历,但可以直接打印**
1 2 3 4 5 6 7 8
| Map map = new HashMap(); map.put(1, penguin1); map.put(2, penguin2); map.put(3, penguin3); System.out.println(map);
输出 {1=Penguin [name=czh1, id=1, love=100], 2=Penguin [name=czh2, id=2, love=100], 3=Penguin [name=czh3, id=3, love=100]}
|
vector 与 ArrayList的区别
* vector线程安全,ArrayList是重速度,轻安全,线程非安全
* 长度增长时,Vector默认增长一倍,ArrayList增长50%
HashTable 和 HashMap的异同
* 实现原理,功能相同,可以互用
* 主要区别
* HashTable 继承Dictionary类,hashMap实现Map接口
* HashTables线程安全,HashMap线程非安全
* HashTable 不允许null值,HashMap允许null
iterator
- 两种遍历方式(iterator 和 增强型for循环)
1 2 3 4
| Iterator it = map.keySet().iterator(); while(it.hasNext()){ syso(it.next()) }
|
1 2 3
| for(Object ob:map.values()){ syso(ob); }
|
1 2 3
| for(Iterator it = map.values().iterator();it.hasNext();){ System.out.println(it.next()); }
|
- map不可以获取iterator(),其他的list类和set都可以通过iterator来遍历
总结:
* 如何选定集合?
* 如何判断他们能执行的方法?
* 通过存储方式!!!
泛型的作用
- 类型限定
- 取出来的元素不用做强制转换了
2.toString()方法可直接生成,打印属性
3.打架包批文件
4.知识在项目会用才算会用
5.文件
java.io.file方法:
- exists()
- isFile()
- isDirectory()
- getName()
- getPath()返回相路径
- getAbsolutePath()返回绝对路径
- length()字节为单位 如果不存在返回OL
- createNewFile()
- delete()
路径
- File file = new File(“hello.txt”)
- File file = new File(“e:\hello.txt”)
System.getProperty(“user.dir”);
获取当前项目路径
_Users_czh_Documents_workspace/Class628
读写文件
流:一连串流动的字符,是以先进先出方式发送消息的通道
输入输出流相对于计算机内存来说
分类
* 按照流向分
* InputStream Reader 都是抽象类
* OutputStream Writer
* 按照处理单位分
* 字符流
* 字节流
字节流是8位通用字节流
字符流是16位Unicode字符流
FileInputStream fio = new FileInputStream(“hello.txt”);
不能直接用InputStream(outStream)他们是抽象类
* fio.read()
* 对一个字节
* fio.read(byte []b)
* **返回读出的字节数 而不是数据本身**
* 最大长度为b.length
* 没有读出返回-1
* fio.read(byte []b , int off , int len)
* 写入位置从off开始
* 读入的个数为len个
FileOutputStream写
* fio.write()
* 写一个字节
* fio.write(byte []b)
* 无返回
* **if(fio.write(b) != -1){}这的写是错的**
* fio.write(byte []b , int off ,int len )
* 无返回
**不要对同一个文件同时读 写**
1 2 3 4 5 6
| byte []b = new byte[1024]; int i = fio.read(b) while(i != -1){ i = fio.read(b); }
|
FileReader 字符读
1 2 3 4 5 6 7 8 9 10
| StringBuffer sb = new StringBuffer(); FileReader reader = new FileReader(“hello.txt”); BufferedReader br = new BufferedReader(reader); String str= br.readLine(); while(str! = null){ sb.append(str); str = br.readline(); } br.close(); Reader.close();
|
FileWriter 字符写 (这里不要写成FIileWrite)
1. 第二个参数 = true 可以设置成追加
2. bw.flush(); 写的时候必须刷新缓存
3. bw.newLine();
4. close 注意:**关闭流时的原则:先开后关,后开的先关**
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
public class FileReaderTest {
public static void main(String[] args) throws Exception {
FileWriter fw = new FileWriter("hello4.txt"); BufferedWriter bw = new BufferedWriter(fw); FileReader fr = new FileReader("hello3.txt"); BufferedReader br = new BufferedReader(fr); String string = new String(); string = br.readLine(); while(string != null){ string = string.replace("{name}", "小花") .replace("{type}", "猫") .replace("{master}","czh"); bw.write(string); bw.flush(); bw.newLine(); string = br.readLine(); } br.close(); fr.close(); bw.close(); fw.close(); }
public static void file(String name) throws IOException{ File file = new File(name); if(file.exists()){ System.out.println(file.getName()); System.out.println(file.getAbsolutePath()); System.out.println(file.getPath()); } else{ file.createNewFile(); System.out.println("已经创建文件"); } } }
|
1. **他们两的参数是FileInputStream()**
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public static void main(String args[]) throws IOException{ DataInputStream dis = new DataInputStream(new FileInputStream("1.jpg")); DataOutputStream dos = new DataOutputStream(new FileOutputStream("2.jpg")); byte[] b = new byte[1024]; int i = dis.read(b); while(i != -1){ dos.write(b,0,b.length); i = dis.read(b); } dos.flush(); System.out.println("完成"); dos.close(); dis.close(); }
|
1 2 3
| public class Pet implements Serializable {
}
|
总结:
1. 一共有四种
1. 文件字节读写 FileInputStream
2. 字符读写 FileReader + BufferedReader
3. 二进制读写 FileInputStram + DataInputStream
2. 二进制读写是万能的方法,不用管复制的文件是啥。
练习(用文件实现用户登陆系统-有数据登陆,没数据添加)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| public static void main(String[] args) throws IOException { String name; String pwd; String rname; String rpwd; String str[] = new String[10]; FileReader fr = new FileReader("hello1.txt"); BufferedReader br = new BufferedReader(fr); String string = new String(); string = br.readLine(); int i = 0; while(string != null){ str[i++]=string; string = br.readLine(); } br.close(); fr.close(); Scanner scan = new Scanner(System.in); System.out.println("请输入用户名"); name = scan.next(); System.out.println("请输入密码"); pwd = scan.next(); String rstr[] = new String[2]; int flag = 0;
for(int j = 0 ;j < i;j++){ rstr = str[j].split(" "); if(rstr[0].equals(name) && rstr[1].equals(pwd)){ flag = 1; break; } } if(flag == 1){ System.out.println("登陆成功"); } else{ FileWriter fw = new FileWriter("hello1.txt",true); BufferedWriter bw = new BufferedWriter(fw); bw.newLine(); bw.write(name+" "+pwd); bw.flush(); bw.close(); fw.close(); System.out.println("登陆失败,数据已写入"); } }
|
6.报错
Unreachable code
原因:return 之后写代码
char 不能写成 Char
7.StringBuffer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| package package1;
public class Test2 {
public static void main(String[] args) { String string[] = new String[10]; System.out.println("string[0]的值:"+string[0]); string[0] = "abc"; System.out.println("string[10]的长度:"+string.length); StringBuffer sb[] = new StringBuffer[10]; System.out.println("stringBuffer[0]得值:"+sb[0]); sb[0] = new StringBuffer(); sb[0].append("abc"); System.out.println("stringbuffer[10]的长度:"+sb.length); StringBuffer sb1 = new StringBuffer();
sb1.append("开始"); method_buffer(sb1); System.out.println("stringbuffer 引用传参:"+sb1); String string1 = new String(); string1 = "开始"; method_string(string1); System.out.println("string 值传参:"+string1); Student student = new Student(); method_student(student); System.out.println("student 引用传参"+student.getAge()); } public static void method_buffer(StringBuffer sb){ sb.append("abc"); } public static void method_string(String string){ string = string+"abc"; } public static void method_student(Student student){ student.setAge(20); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package package1;
public class Student { private int age = 0;
public int getAge() { return age; }
public void setAge(int age) { this.age = age; } }
|
1 2 3 4 5 6 7 8
| 输出: string[0]的值:null string[10]的长度:10 stringBuffer[0]得值:null stringbuffer[10]的长度:10 stringbuffer 引用传参:开始abc string 值传参:开始 student 引用传参20
|
总结:String对象是值传递,其他的对象(stringbuffer+自定义student)都是引用传递