[英]Android JAVA how to check if some index of char array is empty
我正在使用Java中的字符數組。 數組的中間可能缺少一些條目,我需要知道最左邊的索引為空
我的代碼是
private int checkMissingEntry(){
int p = 0;
for(int i = characterArray.length - 1; i >= 0; i--){
if (characterArray[i] == ' '){
p = i;
}
else{
}
}
return p;
}
但是, characterArray[i] == ' '
不會檢測到空字符,代碼始終返回最初分配給p的內容,在這種情況下為0。if語句永遠不會執行。
我嘗試了'\\0'
和'\ '
但似乎都沒有。
解決辦法是什么?
為什么在到達第一個丟失字符后不從左到右搜索並立即返回索引?
編輯:包含缺少值的示例數組
private int checkMissingEntry(){
char[]characterArray = new char[4]; // 4 characters in 1 array
characterArray[0] = 'a'; //but we'll only set values for 3 of them
characterArray[2] = 'b'; //so: index 1 is your empty character
characterArray[3] = 'd';
for(int i = 0; i < characterArray.length ; i++){
char c = characterArray[i]; //get the character at index i
if(c =='\0' || c =='\u0000'){ //check for empty character
return i; //return index
}
} return -1; //only returns -1 if characterArray does not contain the empty character
}
private int checkMissingEntry(){
String tmp = new String(characterArray);
if (tmp != null)
return tmp.lastIndexOf(" ");
return -1;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.