static boolean compstr(String s1,String s2)
{
if(s1.length()!=s2.length()){return false;}
for(int i=0;i<s1.length();i++)
{
if(s1.charAt(i)!=s2.charAt(i)) return false;
}
return true;
}

static int findseqbyID(Vector<Cseq> seqs,String tfseqID)
{
int i;
int n=seqs.size();
/*
//debug
System.out.println("\n\n\n---------------------------");
System.out.println("intf:|"+tfseqID+"|");
//end of debug
*/
for(i=0;i<n;i++)
{
/*
//debug
System.out.print("in:|"+(seqs.get(i)).seqID+"|");
System.out.print("<-->|"+tfseqID+"|\t:");
System.out.println(compstr((seqs.get(i)).seqID,tfseqID));
//System.out.println(((Cseq)seqs.get(i)).seqID==tfseqID);
//end of debug
*/
//if((seqs.get(i)).seqID==tfseqID)
if(compstr((seqs.get(i)).seqID,tfseqID))
{
//debug{System.out.println("found!");}//end of debug
return i;
}
}
return -1;
}
static Vector<Cseq> readalnseq(String strifseq)
{
Vector<Cseq> seqs=new Vector<Cseq>();
Cseq ts=null; 
String strt,strseqID,strseq;
Pattern pseqn=Pattern.compile("^((\\w){1,}) {1,}((\\w|-){1,})$");
Matcher mtht;

//int i;
int pos;
try
{
BufferedReader fin=new BufferedReader(new FileReader(strifseq));

while((strt=fin.readLine())!=null)
{
mtht=pseqn.matcher(strt);
if(mtht.matches())//new seq started
{
//System.out.println("Matched!");
//System.out.println(strt);
strseqID=mtht.group(1);
strseq=mtht.group(3);
pos=findseqbyID(seqs,strseqID);
System.out.println("|"+strseqID+"|");
if(pos==-1)//not added yet
{
System.out.println("new");
ts=new Cseq();
ts.seq=strseq;
ts.seqIntro="";
ts.seqID=strseqID;
seqs.add(ts);
}
else
{
System.out.println("found");
ts=seqs.get(pos);
ts.seq+=strseq;
}
}//end if matches()
//System.out.println(strt);
}//end while
fin.close();
}
catch(IOException e){System.out.println(e);}

//System.out.println(seqs.size());
return seqs;
}

}static Vector<Cseq> readalnseq(String strifseq)
是用来读蛋白质序列的,输入文件是由序列ID和序列组成的,基本上是这样的Os_PHR1_5       -------MSSQSVVAVKQITAPDKIVETCPSTKHSAHKLFDVKPDFQGLIDDN---LSSS
Os_PHR1_6       -------MSTQSVIAVKQFSGPDKIAQAYTVPQPSAHVLSNANYDYDLCGSTNSTSLSCA
AtPHR1_4        -MYIKAIMNRHRLLSAATDECNKKLGQACSSSLSPVHNFLNVQPEHR-------------
Os_PHR1_4       --------------------MLQDIMNTKKIKLHDCHFGSPLCDPSP-------------
AtPHR1_2        --MASTVSVQPGRIRILKKGSWQPLDQTVGPVVYWMFRDQRLKDNWALIHAVDLAN----序列ID是重复出现的,每读入一行,分离出序列ID,到已经读入的序列中去找是否已经存在,如果找到就加到已有序列的末尾,否则作为新序列加入到Vector中.
    问题出在查找序列ID是否已经存在的时候.static int findseqbyID(Vector<Cseq> seqs,String tfseqID),这个函数把待查找的seqID和Vector中已有的作比较,就是比较字符串了.当用if((seqs.get(i)).seqID==tfseqID)来比较时,总是返回-1(即没找到).于是我自己写了个比较函数static boolean compstr(String s1,String s2),这样就能正常工作了.
    
    想请教大家,为什么用==比较就会认为不相同呢?调试时输出它们的值,看上去是完全一样的啊...    谢谢~~~

解决方案 »

  1.   

    对了,附上Cseq的定义: static class Cseq {
    String seqID;
    String seqIntro;
    String seq;
    }
      

  2.   

    JAVA比较字符串的值使用:String str1="Hello World!";
    String str2="Hello World!";
    //比较字符串的值
    System.out.println(str1.equals(str2));//输出true
    //比较两个字符串的hashcode,默认是内存地址
    System.out.println(str1==str2);//输出false
      

  3.   

    回2楼的,我试过equals,也不行的.
    但是这段代码:
    String s1="asdf";
    String s2="asdf";
    System.out.println(s1==s2);
    为什么输出的是true呢?
      

  4.   

    那个,2楼,你的代码在我机器上运行都是true.
      

  5.   

    String str1="Hello World!";
    String str2=new String("Hello World!");
    //比较字符串的值
    System.out.println(str1.equals(str2));//输出true
    //比较两个字符串的hashcode,默认是内存地址
    System.out.println(str1==str2);//输出false
      

  6.   

    如果是String str1="Hello World!";
    这样这样初始化字符串,字符串是存在字符串池中,所以他们对应的地址(hashcode)一样
      

  7.   

    哇~~~~
    如果是String str1="Hello World!"; 
    这样这样初始化字符串,字符串是存在字符串池中,所以他们对应的地址(hashcode)一样这个应该就是我想要的答案了~~~
    能再俱体些吗?什么是对象池?
      

  8.   

    public static void main(String[] args) {
    String str1 = "Hello World!";
    String str2 = "Hello World!";
    System.out.println(str1.hashCode());
    System.out.println(str2.hashCode());
    // 比较字符串的值
    System.out.println(str1.equals(str2));// 输出true
    // 比较两个字符串的hashcode,默认是内存地址
    System.out.println(str1 == str2);// 输出true
    String str3 = "Hello World!";
    String str4 = new String("Hello World!");
    System.out.println(str3.hashCode());
    System.out.println(str4.hashCode());
    // 比较字符串的值
    System.out.println(str3.equals(str4));// 输出true
    // 比较两个字符串的hashcode,默认是内存地址
    System.out.println(str3 == str4);// 输出false
    }
    控制台结果:
    -969099747
    -969099747
    true
    true
    -969099747
    -969099747
    true
    false