在Java编程中,有些知识 并不能仅通过语言规范或者标准API文档就能学到的。在本文中,我会尽量收集一些最常用的习惯用法,特别是很难猜到的用法。(Joshua Bloch的《Effective Java》对这个话题给出了更详尽的论述,可以从这本书里学习更多的用法。)
创新互联公司是专业的文圣网站建设公司,文圣接单;提供成都做网站、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行文圣网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
我把本文的所有代码都放在公共场所里。你可以根据自己的喜好去复制和修改任意的代码片段,不需要任何的凭证。
实现equals()
- class Person {
- String name;
- int birthYear;
- byte[] raw;
- public boolean equals(Object obj) {
- if (!obj instanceof Person)
- return false;
- Person other = (Person)obj;
- return name.equals(other.name)
- && birthYear == other.birthYear
- && Arrays.equals(raw, other.raw);
- }
- public int hashCode() { ... }
- }
实现hashCode()
- class Person {
- String a;
- Object b;
- byte c;
- int[] d;
- public int hashCode() {
- return a.hashCode() + b.hashCode() + c + Arrays.hashCode(d);
- }
- public boolean equals(Object o) { ... }
- }
实现compareTo()
- class Person implements Comparable
{ - String firstName;
- String lastName;
- int birthdate;
- // Compare by firstName, break ties by lastName, finally break ties by birthdate
- public int compareTo(Person other) {
- if (firstName.compareTo(other.firstName) != 0)
- return firstName.compareTo(other.firstName);
- else if (lastName.compareTo(other.lastName) != 0)
- return lastName.compareTo(other.lastName);
- else if (birthdate < other.birthdate)
- return -1;
- else if (birthdate > other.birthdate)
- return 1;
- else
- return 0;
- }
- }
实现clone()
- class Values implements Cloneable {
- String abc;
- double foo;
- int[] bars;
- Date hired;
- public Values clone() {
- try {
- Values result = (Values)super.clone();
- result.bars = result.bars.clone();
- result.hired = result.hired.clone();
- return result;
- } catch (CloneNotSupportedException e) { // Impossible
- throw new AssertionError(e);
- }
- }
- }
使用StringBuilder或StringBuffer
- // join(["a", "b", "c"]) -> "a and b and c"
- String join(List
strs) { - StringBuilder sb = new StringBuilder();
- boolean first = true;
- for (String s : strs) {
- if (first) first = false;
- else sb.append(" and ");
- sb.append(s);
- }
- return sb.toString();
- }
生成一个范围内的随机整数
- Random rand = new Random();
- // Between 1 and 6, inclusive
- int diceRoll() {
- return rand.nextInt(6) + 1;
- }
使用Iterator.remove()
- void filter(List
list) { - for (Iterator
iter = list.iterator(); iter.hasNext(); ) { - String item = iter.next();
- if (...)
- iter.remove();
- }
- }
返转字符串
- String reverse(String s) {
- return new StringBuilder(s).reverse().toString();
- }
启动一条线程
下面的三个例子使用了不同的方式完成了同样的事情。
实现Runnnable的方式:
- void startAThread0() {
- new Thread(new MyRunnable()).start();
- }
- class MyRunnable implements Runnable {
- public void run() {
- ...
- }
- }
继承Thread的方式:
- void startAThread1() {
- new MyThread().start();
- }
- class MyThread extends Thread {
- public void run() {
- ...
- }
- }
匿名继承Thread的方式:
- void startAThread2() {
- new Thread() {
- public void run() {
- ...
- }
- }.start();
- }
使用try-finally
I/O流例子:
- void writeStuff() throws IOException {
- OutputStream out = new FileOutputStream(...);
- try {
- out.write(...);
- } finally {
- out.close();
- }
- }
锁例子:
- void doWithLock(Lock lock) {
- lock.acquire();
- try {
- ...
- } finally {
- lock.release();
- }
- }
从输入流里读取字节数据
- InputStream in = (...);
- try {
- while (true) {
- int b = in.read();
- if (b == -1)
- break;
- (... process b ...)
- }
- } finally {
- in.close();
- }
从输入流里读取块数据
- InputStream in = (...);
- try {
- byte[] buf = new byte[100];
- while (true) {
- int n = in.read(buf);
- if (n == -1)
- break;
- (... process buf with offset=0 and length=n ...)
- }
- } finally {
- in.close();
- }
从文件里读取文本
- BufferedReader in = new BufferedReader(
- new InputStreamReader(new FileInputStream(...), "UTF-8"));
- try {
- while (true) {
- String line = in.readLine();
- if (line == null)
- break;
- (... process line ...)
- }
- } finally {
- in.close();
- }
向文件里写文本
- PrintWriter out = new PrintWriter(
- new OutputStreamWriter(new FileOutputStream(...), "UTF-8"));
- try {
- out.print("Hello ");
- out.print(42);
- out.println(" world!");
- } finally {
- out.close();
- }
预防性检测(Defensive checking)数值
- int factorial(int n) {
- if (n < 0)
- throw new IllegalArgumentException("Undefined");
- else if (n >= 13)
- throw new ArithmeticException("Result overflow");
- else if (n == 0)
- return 1;
- else
- return n * factorial(n - 1);
- }
预防性检测对象
- int findIndex(List
list, String target) { - if (list == null || target == null)
- throw new NullPointerException();
- ...
- }
预防性检测数组索引
- void frob(byte[] b, int index) {
- if (b == null)
- throw new NullPointerException();
- if (index < 0 || index >= b.length)
- throw new IndexOutOfBoundsException();
- ...
- }
预防性检测数组区间
- void frob(byte[] b, int off, int len) {
- if (b == null)
- throw new NullPointerException();
- if (off < 0 || off > b.length
- || len < 0 || b.length - off < len)
- throw new IndexOutOfBoundsException();
- ...
- }
填充数组元素
使用循环:
- // Fill each element of array 'a' with 123
- byte[] a = (...);
- for (int i = 0; i < a.length; i++)
- a[i] = 123;
(优先)使用标准库的方法:
- Arrays.fill(a, (byte)123);
复制一个范围内的数组元素
使用循环:
- // Copy 8 elements from array 'a' starting at offset 3
- // to array 'b' starting at offset 6,
- // assuming 'a' and 'b' are distinct arrays
- byte[] a = (...);
- byte[] b = (...);
- for (int i = 0; i < 8; i++)
- b[6 + i] = a[3 + i];
(优先)使用标准库的方法:
- System.arraycopy(a, 3, b, 6, 8);
调整数组大小
使用循环(扩大规模):
- // Make array 'a' larger to newLen
- byte[] a = (...);
- byte[] b = new byte[newLen];
- for (int i = 0; i < a.length; i++) // Goes up to length of A
- b[i] = a[i];
- a = b;
使用循环(减小规模):
- // Make array 'a' smaller to newLen
- byte[] a = (...);
- byte[] b = new byte[newLen];
- for (int i = 0; i < b.length; i++) // Goes up to length of B
- b[i] = a[i];
- a = b;
(优先)使用标准库的方法:
- a = Arrays.copyOf(a, newLen);
把4个字节包装(packing)成一个int
- int packBigEndian(byte[] b) {
- return (b[0] & 0xFF) << 24
- | (b[1] & 0xFF) << 16
- | (b[2] & 0xFF) << 8
- | (b[3] & 0xFF) << 0;
- }
- int packLittleEndian(byte[] b) {
- return (b[0] & 0xFF) << 0
- | (b[1] & 0xFF) << 8
- | (b[2] & 0xFF) << 16
- | (b[3] & 0xFF) << 24;
- }
把int分解(Unpacking)成4个字节
- byte[] unpackBigEndian(int x) {
- return new byte[] {
- (byte)(x >>> 24),
- (byte)(x >>> 16),
- (byte)(x >>> 8),
- (byte)(x >>> 0)
- };
- }
- byte[] unpackLittleEndian(int x) {
- return new byte[] {
- (byte)(x >>> 0),
- (byte)(x >>> 8),
- (byte)(x >>> 16),
- (byte)(x >>> 24)
- };
- }
本文题目:关于Java一些习惯用法的总结
网页地址:http://www.csdahua.cn/qtweb/news5/240705.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网