Problem using if statement with ArrayList
I am using an if statement as shown below,
if(sign.size()==0)
Here sign
is of the type ArrayList<Character>
I am trying to add a char
to the ArrayList
But its not working. Is there anything wrong with my if statement?
I also tried the same with an ArrayList<doubler>
, this time I could get into the if statement.
Is there anything wrong with the if stateme开发者_StackOverflownt?
I am having very difficult time understanding what you are saying, but it sounds like you are trying to figure out how to increase the list capacity. You do not need to manually do this. Simply add items using add() method and the list will re-size itself as appropriate.
Try if(sign.isEmpty())
instead. Also make sure you you're using good code style with {'s where needed. ArrayLists will adjust their own size to accommodate what you put in, so I'm not sure you even need this check.
You commented on an answer and said,
i want to get in the if only if i have 1 char in the array... but i does not get in to the if.... way if i have only one index
If you want to enter the IF
only when there is one char, then you'd want
IF(sign.size() == 1){
...some code
}
Otherwise, if you want to enter the IF
when size is 0, use isEmpty.
As you said
I want to get in the if only if I have 1 char in the array... but it does not get in to the if.... what if i have only one index?
If you are trying to enter the if statement only when there is only one char
in the ArrayList then you can use
if(sign.size() == 1){
//your code
}
The condition will only be true when the size of the ArrayList
is equal to 1.
精彩评论