首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java数组越界有关问题

2012-06-09 
java数组越界问题package com.testimport java.io.BufferedReaderimport java.io.BufferedWriterimport

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); 


m.put(k, v); 



Set<Entry<String, Integer>> s = m.entrySet(); 
Entry<String, Integer>[] es = s.toArray(new Map.Entry[] { null }); 

java.util.Arrays.sort(es, new Comparator() 

public int compare(Object o1, Object o2) 

int e1 = ((Map.Entry<String, Integer>) o1).getValue() 
.intValue(); 
int e2 = ((Map.Entry<String, Integer>) o2).getValue() 
.intValue(); 

if (e1 > e2) 

return 1; 

else if (e1 < e2) 

return -1; 

return 0; 

}); 

return es; 


public static void printAnyone(Entry<String, Integer>[] es) 

IPtest ipTest = new IPtest();
String path = ipTest.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
if (path.indexOf("WEB-INF") > 0) {
path = path.substring(1, path.indexOf("/WEB-INF/"));
}
path+="/qqwry";
// 指定纯真数据库的文件名,所在文件夹
IPSeeker ip = new IPSeeker("QQWry.Dat", path);
// 测试IP 58.20.43.13
System.out.println(path);
for (int i = 0; i < es.length; i++) 

System.out.println(es[i].getKey() + "出现过 " + es[i].getValue() + "次"); 

System.out.println(ip.getIPLocation(es[i].getKey().trim()).getCountry());
System.out.println(ip.getIPLocation(es[i].getKey().trim()).getArea());







try {
File directory = new File(".");  
directory.getCanonicalPath();//取得当前路径
String no1 =System.getProperty("user.dir");

File f = new File(directory+directory.separator+"ip.txt");
//用FileOutputSteam包装文件,并设置文件可追加
OutputStream out;
out = new FileOutputStream(f,true);
//int j=0;
//for(j=0;j<arr.length;j++){
 
out.write(ip.getIPLocation(es[i].getKey().trim()).getCountry().getBytes()); //向文件中写入数据
out.write(ip.getIPLocation(es[i].getKey().trim()).getArea().getBytes()); //向文件中写入数据
out.write(es[i].getValue().toString().getBytes());
out.write('\r'); // \r\n表示换行
out.write('\n'); 
 
out.close();//关闭输出流
System.out.println("写入成功!");

}catch(IOException e){
e.printStackTrace();
}


}

}
public static void printMax(Entry<String, Integer>[] es) 
{  

Entry<String, Integer> max = es[es.length - 1]; 
IPtest ipTest = new IPtest();
String path = ipTest.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
if (path.indexOf("WEB-INF") > 0) {
path = path.substring(1, path.indexOf("/WEB-INF/"));
}
path+="/qqwry";
// 指定纯真数据库的文件名,所在文件夹
IPSeeker ip = new IPSeeker("QQWry.Dat", path);
// 测试IP 58.20.43.13
System.out.println(path);
System.out.println(max.getKey() + "出现过" + (Integer) max.getValue()+"次"); 
System.out.println(ip.getIPLocation(max.getKey().trim()).getCountry());
System.out.println(ip.getIPLocation(max.getKey().trim()).getArea());

for (int i = es.length - 2; i > -1; i--) 

if (es[i].getValue().intValue() == max.getValue().intValue()) 

System.out.println((String) es[i].getKey() + "出现过" + (Integer) es[i].getValue() + "次"); 


else 

break; 



 
 


 

 




 
}


   



[解决办法]

探讨

一大堆,还要别人去看,累啊
直接说问题在哪,或者给出错误提示

热点排行