java基础回顾与加强004

上传人:xiao****1972 文档编号:83805924 上传时间:2019-03-01 格式:DOC 页数:24 大小:227.50KB
返回 下载 相关 举报
java基础回顾与加强004_第1页
第1页 / 共24页
java基础回顾与加强004_第2页
第2页 / 共24页
java基础回顾与加强004_第3页
第3页 / 共24页
java基础回顾与加强004_第4页
第4页 / 共24页
java基础回顾与加强004_第5页
第5页 / 共24页
点击查看更多>>
资源描述

《java基础回顾与加强004》由会员分享,可在线阅读,更多相关《java基础回顾与加强004(24页珍藏版)》请在金锄头文库上搜索。

1、一、 基础回顾1. 集合1.1. 集合的类型与各自的特性-|Collection: 单列集合-|List: 有存储顺序, 可重复-|ArrayList:数组实现, 查找快, 增删慢 由于是数组实现, 在增和删的时候会牵扯到数组增容, 以及拷贝元素. 所以慢。数组是可以直接按索引查找, 所以查找时较快-|LinkedList:链表实现, 增删快, 查找慢由于链表实现, 增加时只要让前一个元素记住自己就可以, 删除时让前一个元素记住后一个元素, 后一个元素记住前一个元素. 这样的增删效率较高但查询时需要一个一个的遍历, 所以效率较低-|Vector:和ArrayList原理相同, 但线程安全, 效

2、率略低 和ArrayList实现方式相同, 但考虑了线程安全问题, 所以效率略低-|Set: 无存储顺序, 不可重复-|HashSet 线程不安全,存取速度快。底层是以哈希表实现的。-|TreeSet 红-黑树的数据结构,默认对元素进行自然排 序(String)。如果在比较的时候两个对象 返回值为0,那么元素重复。-| Map: 键值对 键不可重复,键可以重复-|HashMap 线程不安全,存取速度快。底层是以哈希表实现的.-|TreeMap 红-黑树的数据结构,默认对元素进行自然排 序(String)。如果在比较的时候两个对象 返回值为0,那么元素重复-|HashTable 底层也是使用了哈

3、希表 维护的,存取的读取快,存储元素是 无序的。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+) Sy

4、stem.out.println(arri);1.2.3. 遍历Listpublic static void main(String args) / 遍历List:List list = new ArrayList();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 (Iterato

5、r iter = list.iterator(); iter.hasNext();) String elt = iter.next();System.out.println(elt);1.2.4. 遍历Setpublic static void main(String args) / 遍历Set:Set set = new HashSet();set.add(dd);set.add(ee);set.add(ff);/ 1,增强的for循环for (String elt : set) System.out.println(elt);/ 2,迭代器for(Iterator iter = set.i

6、terator(); iter.hasNext() ; )String elt = iter.next();System.out.println(elt);1.2.5. 遍历Mappublic static void main(String args) / 遍历Map:Map map = new HashMap();map.put(aa, xx);map.put(bb, yy);map.put(cc, zz);/ 1,增强的for循环(Entry集合)for (Entry entry : map.entrySet() System.out.println(entry);/ 2,增强的for循环

7、(Key集合)for(String key : map.keySet()System.out.println(key + = + map.get(key);/ 3,遍历值的集合for(String value : map.values()System.out.println(value);2. IO流2.1. IO流的分类输入流输出流说明字节流InputStreamOutputStream字节流是处理字节的(二进制)字符流ReaderWriter字符流是处理字符的注:这几个类都是抽象类。2.2. 读文件的代码public static void main(String args) String

8、 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 Run

9、timeException(e);2.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 byte1024 * 8;for (int len

10、 = -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);3. 多线程3

11、.1. 启动线程方式1, 自定义的类继承Thread类。使用代码为new MyThread().start()2,自定义的类实现Runnable接口。使用代码为new Thread(new MyRunnable().start3.2. 代码以下代码是分别用两种方式启动线程(还是用到了匿名内部类)private static int count = 100;public static void main(String args) / 用继承Thread类的方式启动一个线程new Thread() public void run() synchronized (StartThreadTest.cl

12、ass) 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();4. Socket网络编程4.1. 网络通讯的三要素IP地址Port 端口号通讯方式: TCP 或 UDP4.2. TCP服务器端代码public class MyTcpServer public static int port = 8081;public static i

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号