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 method
		char[] 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 convert
		destClone = (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 SimpleDateFormat
		Date 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 SimpleDateFormat
		String strDate = "2011年2月28日";
		//Attention:SimpleDateFormat constructure function style must agree with convertStringToDate
		SimpleDateFormat 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中元素拷贝到ArrayList
	Collections.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});

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(java)