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常用类,java封装类,String类和StringBuffer类......
本专栏主要为Java程序设计(基础)实验报告和Java程序设计(进阶)实验报告,基础篇有JAVA环境搭建、Java语言基础、方法和数组、面向对象基础、Java常用类、继承与接口、成员访问控制与异常、JavaFX程序设计、Java...
常用类的详细笔记常用类的详细笔记常用类的详细笔记常用类的详细笔记常用类的详细笔记
Java常用类.pptJava常用类.ppt
JAVA常用类和方法 可以随时翻阅查询 加快开发
Java常用类,让你对常用类有个全面的了解,有的放矢,加油
关于Java 常用类讲解,包括List接口和ArrayList类 Map接口和HashMap类 Properties类
java 常用类课件。Collection、List、LinkedList、Vector、Iterator、Map、HashMap、TreeMap、MATH、Date、Set
Java常用类总结: 总结了java中各种常用的类以及方法的使用
适合初学者 此文本包含java中的常用类
Java 常用类 源码
这是java常用类解析系列博客中的示例代码及自己写的工具类,代码注释详细,博客地址:http://blog.csdn.net/touch_2011/article/details/6860043 主要讲解了System类、Object类、Arrays类、Cloneable接口、IO系统...
Java常用类 Math类 String类 StringBuffer类 StringTokenizer类 包装类 集合操作 集合 列表(List) 映射(Map) Collections类 枚举和迭代
对于初学者会有很大帮助,pdf文件,java常用类,java常用类
java 常用类 ppt 字符串相关类 基本数据包装类 Math类 File类
java常用类的讲解,这些类是javase 以及后边的javaee中的常用的java类,是学习java的基础。