ITeye博客

Tinking in java chapter

共 8 篇文章

Arrays.sort数组排序

使用Arrays.sort就可以对数组进行排序,即可以对任意的基本类型进行排序,又可以对任意的对象数组进行排序,只要该对象实现了Comparable接口或者有相关联的Comparator. JAVA标准库的排序算法针对正排序的特殊类型进行了优化,针对基本类型设计了“快速排序”,针对对象设计的“稳定归并排序”。所以无需担心性能。 package com.liuc.test.think.chapter16; import java.util.Arrays; public class ArraySort { /** * @param args */ public static void main(…

2012-08-27 22:53 · Tinking in java chapter

System.arraycopy Arrays.equals

package com.liuc.test.think.chapter16; import java.util.Arrays; public class CopyingArray { /** * @param args */ public static void main(String[] args) { int[] i=new int[7]; int[] j=new int[10]; Arrays.fill(i, 47); Arrays.fill(j, 99); System.out.println(Arrays.toString(i)); System.out.println(Array…

2012-08-27 22:14 · Tinking in java chapter

Arrays.fill()填充方法

Arrays.fill()功能很有限,只能用同一个值填充各个位置,他有两个比较典型的方法: public static void fill(boolean[] a, boolean val) public static void fill(boolean[] a, int fromIndex, int toIndex,boolean val) 第一个方法是把数组中所有的元素用val元素填充 第二个是把指定位置的元素用val填充,填充的位置包括fromIndex,不包括toIndex index下标是从0开始 其实第一个方法是调用第二个方法的实现: public static void fil…

2012-08-27 21:20 · Tinking in java chapter · Java

java动态类型安全

[size=medium]当想集合中插入一个组数据的时候,除了可以明确的指定类型(如List)外,还可以使用java.util.Collections提供的方法来检查尅性。包括的静态方法有[/size] [code="java"] checkedCollection(Collection c, Class type) checkedList(List list, Class type) checkedMap(Map m, Class keyType, Class valueType) checkedSet(Set s, Class type) checkedSortedMap(SortedM…

2012-08-14 23:15 · java基础 · Tinking in java chapter · 泛型

创建一个同种类型数据的数组

[size=medium]使用 Array.newInstance(kind, size);可以按照指定类型和长度,创建一个指定类型的数组 对于在泛型中创建数组,使用 Array.newInstance(kind, size)是推荐的方式 [/size] [code="java"] package com.liuc.test.think.chapter15; import java.lang.reflect.Array; import java.util.Arrays; public class ArrayMaker { private Class kind; public ArrayMak…

2012-08-08 22:03 · Tinking in java chapter · java

一个Thiking泛型的例子,突然发现理解了List<String>的原理

[code="java"] package com.liuc.test.think.chapter15; public class LinkedStack { private static class Node{ U item; Node next; Node(){ item=null; next=null; } Node(U item,Node next){ this.item=item; this.next=next; } boolean end(){ return item==null&&next==null; } } private Node top=new Node(); publ…

2012-07-26 22:20 · Tinking in java chapter

JAVA动态代理

Thinking in java 的一个动态代理的例子 package com.liuc.test.think.chapter14; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class SimpleDynamicProxy { public static void consumer(Interface inte){ inte.doSomething(); inte.somethingElse("bonb…

2012-07-24 20:54 · Tinking in java chapter

scanner按指定的分割符号读取文件

package com.liuc.test.think.chapter13; import java.util.Scanner; import java.util.regex.Pattern; public class ScannerDelimiter { public static void main(String[] args) { Scanner scanner=new Scanner("12,42,78,99,42"); scanner.useDelimiter("\\s*,\\s*"); //指定分隔符 while(scanner.hasNextInt()){ System.out…

2012-07-18 21:08 · Tinking in java chapter · Java · scanner