The java ternary operator ?
is giving me an error using it in the following way:
ArrayList <String> titleTabs= new ArrayList<>();
ArrayList <String> countTitleTabs= new ArrayList<>();
public ViewPagerAdapter(ArrayList<String> titleTabs, ArrayList<String> countTitleTabs) {
titleTabs.get(0).isEmpty() ? this.titleTabs.add(0,"----") : this.titleTabs.add(0,titleTabs.get(0));
}
What am I doing wrong?
The ternary operator
?
is also known as a conditional assignment . So, use it to assign a value based on a given condition.In your case it would be:
Since the first value is the same in both cases, only the second value changes and the construction of that operator is:
The ternary operator is used to return a value, not to perform operations. Your code should be something like this:
either
In the cases that I have had to use it, I have used it in the following way:
Both
?
and:
must assign a value followed by them.