在java类集合中还有其他的属性操作类,例如Collections类,在Collections类中的方法都是静态方法,
具体用法见如下代码:
package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Iterator;
//Collections类中都是静态方法,可以直接通过类.方法的形式直接调用
public class CollectionDemo {
public static void main(String args[]){
List<String> all=new ArrayList<String>();
Collections.addAll(all, "aa","bb","www.baidu.com","zhoumeixu","sdsda");
int point=Collections.binarySearch(all, "www.baidu.com");
Collections.replaceAll(all, "zhoumeixu", "周美旭");
System.out.println("查到百度的位置是在:"+point);
Collections.reverse(all);
Collections.swap(all, 0, 1); //交换位置
Iterator<String> iter=all.iterator();
while(iter.hasNext()){
System.out.println("List的集合是:"+iter.next());
}
}
}
Properties类是属于Hashtable的子类,既然是子类,则很定按照key和value的形式参访数据的
package test;
import java.util.Properties;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
public class PropertiesDemo {
public static void main(String args[]){
Properties pro=new Properties();
pro.setProperty("BJ", "beijing");
pro.setProperty("TJ", "tianjing");
pro.setProperty("NJ", "nanjing");
System.out.println("1、BJ属性存在:"+pro.getProperty("BJ"));
System.out.println("2、sJ属性存在:"+pro.getProperty("sJ"));
File file=new File("D:"+File.separator+"area.properties");
File file1=new File("D:"+File.separator+"area.xml");
try{
pro.store(new FileOutputStream(file), "area Info");
pro.storeToXML(new FileOutputStream(file1),"area info");
}catch(Exception e){
e.printStackTrace();
}
try{
pro.load(new FileInputStream(file));
}catch(Exception e){
e.printStackTrace();
}
System.out.println(pro.getProperty("BJ"));
}
}