我有一個字符串"sss"
和一個字符數組{'s','s','s'}
。我將該數組轉換爲字符串,但消息「兩者相等」在比較它們時未打印。爲什麼?如何在android中比較字符串和字符數組?
public class RoughActivity extends Activity
{
private Button checkButton;
private TextView text;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start();
}
private void start() {
int i;
checkButton=(Button)findViewById(R.id.go);
text=(TextView)findViewById(R.id.display);
final String question="sss";
final char answer[]=new char[question.length()];
for(i=0;i<question.length();i++)
{
answer[i]='s';
}
checkButton.setOnClickListener(new View.OnClickListener(){ //on clicking the button,the text must be filled with "both are equal"
public void onClick(View v){
if(question.equals(answer))
{
text.setText("both are equal");
}
}});
}
}
你試圖比較一個字符串與一個字符數組是兩個非常不同的東西.. – 2013-03-15 14:58:34