日期:2014-05-20  浏览次数:20867 次

java数组越界问题
package com.test;
import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileNotFoundException
import java.io.FileOutputStream;
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextConvertor2 { 



public static String convertingStr(String lineStr) { 
  String retVal = ""; 
  return retVal; 


public static String readPath = "C:\\Users\\zhangqi\\Downloads\\too9.com.txt\\too9.com.txt"; 
public static String writePath = "D:\\hello.txt"; 





/** 
  * @param args 
  * @throws IOException 
  */ 
public static void main(String[] args) throws IOException { 
  File file = new File(readPath); 

  int index=0;
//List <String> ip3 = new ArrayList<String>(); 

//String[] arr = new String[ip3.size()];


String[] ip3=new String[4100000];//result
  if (!file.exists()) { 
  System.out.println("File not exist!"); 
  System.exit(0); 
  } 
  BufferedReader br = new BufferedReader(new FileReader(readPath)); 
  // BufferedWriter bw = new BufferedWriter(new FileWriter(writePath)); 
  String temp = ""; 
  index =0;
  // String[] arr = ip3.toArray(new String[4111111]);

 while ((temp = br.readLine()) != null) { 

StringTokenizer tok = new StringTokenizer(temp,":");
while(tok.hasMoreTokens()){
String subStr = tok.nextToken();
String number = tok.nextToken();
String ip2 = tok.nextToken();
String door = tok.nextToken();
String number2 = tok.nextToken();
String name2 = tok.nextToken();
String door2 = tok.nextToken();
String one = tok.nextToken();
 
// System.out.println(door);
  //把变量存入数组
 
// ip3.add(door);
 
 
ip3[index++] = door;
// index++;
for (int i=0;i<index;i++){
//System.out.println(i);
 
//System.out.println(ip3[i]);
 
}


}
String[] data = Arrays.copyOfRange(ip3, 0,index);//用另外一个字符串替换。
Map.Entry<String, Integer>[] es = handle(data);
System.out.println("(每个余元素出现过的次数)"); 
printAnyone(es); 
System.out.println("(出现过次数最多的元素)"); 
printMax(es);  
 

 }// end of while


 
 br.close(); 




}// end of main
 
 


public static Entry<String, Integer>[] handle(String[] data) 

Map<String, Integer> m = new HashMap<String, Integer>(); 
for (int i = 0; i < data.length; i++) 

String k = new String(data[i]); 

if (!m.containsKey(k)) 

m.put(k, new Integer(1)); 

else 

Integer v = (Integer) m.get(k); 
v = new Integer(v.intValue() + 1);