首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java惯用类

2012-12-21 
Java常用类1:反序:public static void reverseString (String str){for (int i str.length() i 0 i-

Java常用类

1:反序:

public static void reverseString (String str){for (int i = str.length(); i > 0; i--) {System.out.print(str.charAt(i -1));}//use array methodchar[] data = str.toCharArray();for (int i = data.length; i>0; i--) {System.out.print(data[i - 1]);}}

?

2:数组拷贝:

//Java array copy:/*1:use for cycle,lack of efficiency;2:use clone() method,get value of array[],not cite(quote),can not copy appoint element,lack of flexibility;3:use System.arraycopy(src, srcPos, dest, destPos, length) method,?recommended;*///1:use for cycle:int [] source = {1,2,3,4,5};int [] dest = new int[5];for (int i = 0; i < dest.length; i++) {dest [i] = source [i];}//2:use clone():int [] src = {1,2,3,4,5};@SuppressWarnings("unused")int [] destClone;//use clone create and should use force convertdestClone = (int [])src.clone();//3:use System.arraycopy(src, srcPos, dest, destPos, length);//src:源数组, srcPos:源数组要复制的起始位置, dest:目标数组, destPos:目标数组放置的起始位置, length:数组复制的长度;int element[] = {1,2,3,4,5,6};int hold [] = new int[6];System.arraycopy(element,0,hold,0,element.length);for (int i = 0; i < hold.length; i++) {System.out.print(hold[i]);}//自我复制System.arraycopy(element,0,hold,3,3);for (int i = 0; i < hold.length; i++) {System.out.print(hold[i]);}String strToken = "1,2,3,4,5";StringTokenizer st = new StringTokenizer(strToken,",",false);while (st.hasMoreTokens()) {String value = st.nextToken();System.out.print(value);}

3:?日期和String相互转化:

//convert Date to String,use the format() method of SimpleDateFormatDate convertDateToString = new Date();SimpleDateFormat sdfDS = new SimpleDateFormat();System.out.println("convert Date to String: "+sdfDS.format(convertDateToString));//convert String to Date,use the parse() method of SimpleDateFormatString strDate = "2011年2月28日";//Attention:SimpleDateFormat constructure function style must agree with convertStringToDateSimpleDateFormat sdfSD = new SimpleDateFormat("yyyy年MM月dd日");try {Date convertStringToDate = sdfSD.parse(strDate);System.out.println("convert String to Date: "+convertStringToDate);} catch (ParseException e) {e.printStackTrace();}

?

Java Collection Framework:

?

List arrayList = new ArrayList();arrayList.add("p");arrayList.add("m");arrayList.add("x");//对已知集合排序Collections.sort(arrayList);System.out.println(arrayList);//对集合进行随机排序//Collections.shuffle(arrayList);//System.out.println(arrayList);Collections.binarySearch(arrayList,"r");//为参数返回一个Enumeration @SuppressWarnings("unused")Enumeration emumList = Collections.enumeration(arrayList);//用对象Object替换list中所有元素Collections.fill(arrayList,new Object());//交换集合中的指定元素位置Collections.swap(arrayList,2,3);//将arrayList中元素拷贝到ArrayListCollections.copy(arrayList,new ArrayList());//集合中元素向后移动n个元素,最后面被覆盖的元素循环移动到前面Collections.rotate(arrayList,1);//将集合中的元素反序Collections.reverse(arrayList);/*It is imperative that the user manually synchronize on the returned     * list when iterating over it:     * <pre>     * List list = Collections.synchronizedList(new ArrayList());     *   ...     *   synchronized(list) {     *     Iterator i = list.iterator(); // Must be in synchronized block     *     while (i.hasNext()){     *       foo(i.next());     *     }     *   }     * </pre>     * Failure to follow this advice may result in non-deterministic behavior.     * <p>The returned list will be serializable if the specified list is serializable.     * @return a synchronized view of the specified list.     *///SynchronizedRandomAccessList,SynchronizedList,SynchronizedCollection都是Collections工具类的static内部类Collections.synchronizedList(arrayList);

?

?关于ArrayList与Vector线程安全,请参考文章:http://overshit.iteye.com/admin/blogs/938659

?

?

Arrays与Collections类似:

Arrays.asList(new int[]{1,2,3,4,5});

?

?

?

?

?

?

?

?

?

?

热点排行