統計資料

ahesihua發表於2011-11-06

今天做了一個統計資料的小程式,用到了arrayList 和array,DOM解析,File檔案和目錄建立:

package config;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class NewTimeDistribution {
	private  List<Integer> costList=new ArrayList<Integer>();
	private int max;
	private int interval=100;
	private int arrayLength;
	
	public int getMax() {
		return max;
	}

	public int getArrayLength() {
		return arrayLength;
	}

	public void setArrayLength(int max) {
		this.arrayLength =max/interval+1;
	}

	public NewTimeDistribution() {
		super();
	}

	
	public List<Integer> getCostList() {
		return costList;
	}

	public  void parseFile(String fileName) throws ParserConfigurationException, SAXException, IOException{
		
		DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder=dbf.newDocumentBuilder();
		Document document=documentBuilder.parse(new File(fileName));
		Element element=document.getDocumentElement();
		NodeList items=element.getElementsByTagName("item");
		
		for(int i=0;i<items.getLength();i++){
			Element item=(Element) items.item(i);
			NodeList results=item.getElementsByTagName("result");
			for(int j=0;j<results.getLength();j++){
				Element result=(Element) results.item(j);
				String tag=result.getAttribute("tag");
				if(!(tag.equals("init-player"))){
					int cost=Integer.parseInt(result.getAttribute("cost"));		
					costList.add(cost);					
				}
			}
		}
	}
	
	public void maxCost(List<Integer> list){
		this.max=list.get(0);
		for(int index=1;index<list.size();index++){
			if(max <list.get(index)){
				max=list.get(index);
			}
		}
	}
		
	public int[] arrayData(List<Integer>  list, int length){				
		int[] arrayCost=new int[length];
		
		for(int index=0;index<list.size();index++){
			arrayCost[list.get(index)/interval]++;
		}
						
		return arrayCost;		
	}
	
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		StringBuilder sb=new StringBuilder();
		NewTimeDistribution ntd= new NewTimeDistribution();
		ntd.parseFile(args[0]);
		List<Integer> costList=ntd.getCostList();
		ntd.maxCost(costList);
		int maxCost=ntd.getMax();
		ntd.setArrayLength(maxCost);
		int arrayLength=ntd.getArrayLength();
		
		int[] arrayData=ntd.arrayData(costList, arrayLength);
		
		for(int i=0;i<arrayData.length;i++){
			System.out.println(100*(i+1)+"\t"+arrayData[i]);
			sb.append(100*(i+1)+"\t"+arrayData[i]+"\n");
		}
		
		File file = new File(args[1]);
		if (!file.exists()) {
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		
		FileOutputStream out = new FileOutputStream(file);
		out.write(sb.toString().getBytes());
		out.flush();
		out.close();
		
	}

}

 

相關文章