我做的就是求一个字符串里面的最大重复字串的长度。比如“do not ask what your country can do for you,ask what you can do for your country” 这个字符串最大字串就是"can do for you",长度是14,程序我已经做出来了,结果都没有问题。
如下MaxRepStr.java:
import java.util.*;
import java.io.*;
public class MaxRepStr{
public static void main(String[] args) throws IOException{
/*FileReader in=null;
in=new FileReader("D:\\javaEE\\workspace\\HelloTest\\bin\\test.txt");
int c;
List<String> strList=new ArrayList<String>();
while((c=in.read())!=-1){
strList.add(String.valueOf((char)c));
}
String str[]=new String[strList.size()];
for(int j=0,len=strList.size();j<len;j++){
str[j]=strList.get(j);
}*/
String str[]={"d","o"," ","n","o","t"," ","a","s","k"," ","w","h","a","t"," ","y","o","u","r"," ","c","o","u","n","t","r","y"," ","c","a","n"," ","d","o"," ","f","o","r"," ","y","o","u",",","a","s","k"," ","w","h","a","t"," ","y","o","u"," ","c","a","n"," ","d","o"," ","f","o","r"," ","y","o","u","r"," ","c","o","u","n","t","r","y"};
System.out.println();
Map<String,List<Integer>> m=new HashMap<String,List<Integer>>();
for(int i=0,len=str.length;i<len;i++){
if(str[i].equals(" ")) continue;
List<Integer> letter=m.get(str[i]);
if(letter==null){
letter=new ArrayList<Integer>();
letter.add(i);
m.put(str[i],letter);
}else{
letter.add(i);
}
}
System.out.println(m);
int maxCount=0;
int end=0;
for(int i=0,len=str.length;i<len;i++){
if(str[i].equals(" ")) continue;
List<Integer> posList=m.get(str[i]);
for(int posi=posList.indexOf(new Integer(i));posi<posList.size()-1;posi++){
int num=0;
int pos=posList.get(posi+1);
for(int j=i,p=pos;j<len&&p<len;){
if(str[j]==str[p]){
j++;
p++;
num++;
if(maxCount<num){
maxCount=num;
end=p-1;
}
}else break;
}
}
}
System.out.println(maxCount);
for(int i=end-maxCount+1;i<=end;i++){
System.out.print(str[i]);
}
}
}
这样运行结果无误。现在我想从下面文本文件test.txt中读取这个字符串,test.txt内容如下(就一行):
do not ask what your country can do for you,ask what you can do for your country我改的其实就是注释里面的内容,把字符串从文本中读出来,然后转成这个数组(String str[]={"d","o"," ","n","o","t"," ","a","s","k"," ","w","h","a","t"," ","y","o","u","r"," ","c","o","u","n","t","r","y"," ","c","a","n"," ","d","o"," ","f","o","r"," ","y","o","u",",","a","s","k"," ","w","h","a","t"," ","y","o","u"," ","c","a","n"," ","d","o"," ","f","o","r"," ","y","o","u","r"," ","c","o","u","n","t","r","y"};),然后把之前定义的那个注释掉。得到的是一模一样的数组啊!别的地方都没变,就是结果不对,好郁闷,想好久也没想明白怎么回事,受不鸟了!求高手帮帮忙~~不胜感激。

解决方案 »

  1.   

    楼主是想问为什么两种取得str[]的方式下运行结果不对吧是这句出了问题:
    if(str[j]==str[p]){应该改成
    if (str[j].equals(str[p])) {字符串之间比较,尽量避免用==,只有两个字符串引用指向内存中的同一个字符串对象,==才会返回true。
    equals则只要两个字符串的内容相等就会返回true,哪怕他们是内存中的两个字符串对象。你的第一种方式的成功只是一个凑巧,因为如
    String str[]={"d","o"," ","n","o","t"," ","a","s","k"," ","w","h","a","t"," ","y","o","u","r"," ","c","o","u","n","t","r","y"," ","c","a","n"," ","d","o"," ","f","o","r"," ","y","o","u",",","a","s","k"," ","w","h","a","t"," ","y","o","u"," ","c","a","n"," ","d","o"," ","f","o","r"," ","y","o","u","r"," ","c","o","u","n","t","r","y"};
    这样的定义,每个字符串都在编译的时候放入常量池,而且为相同内容的字符串只创建了一个对象,这样导致==刚好没有问题。
    方式二从文件读入,字符串对象在运行时由String.valueOf((char) c)创建,哪怕是相同内容的字符串,在内存中也是不同的对象,用==比较是不会相等的。
      

  2.   

    用字符数组String str[]={"d","o"," ...
    改为 char[] c = {'d', 'o' ...
    读取文件,用
    String line = new BufferedReader(new FileInputStream(your_file)).readLine();
    然后
    char c = line.toCharArray();