In Java, if one boolean true then all others become false [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was wondering if it was possible in Java to have a series of booleans such as:
boolean boo1, boo2, boo3, boo4, boo5;
I want to make it so that if one of these booleans become true than all others become false. I know this can be done with a series of long if statements but is there a simpler way to achieve this.
Thanks!

Using an array,
boolean bs = new boolean[10];
lightUp(bs, 6); // only bs[6] will be true after this
lightUp(bs, 0); // only bs[0] will be true after this
With the lightUp function defined as:
/**
* modifies the passed-in array to make sure only
* the selected index is set to true
*/
void lightUp(boolean[] bs, int index) {
for (int i=0; i<bs.length; i++) bs[i] = false;
bs[index] = true;
}

Related

JAVA : Check if Collection of object with start and end dates are overlapping - best way [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I've multiple List of Java-Datetimes with MyCustomDate object contains ( start DateTime and end DateTime ) LocalDateTime with hours and seconds.
I need to write function to check if There is Overlapping dates in my list return true \ false - with the best performance
can use java8 (stream functions)
You have to test all ranges aginst each other. There is no other way. Try this way, although is just one implementation, there are plenty of correct ways to implement it:
public boolean isThereOverlapingRanges(List<CustomDateRange> ranges) {
if (ranges.size() <= 1) {
return false;
}
boolean overlaping = false;
for(int i = 1; i < ranges.size(); i++) {
if (isDateInRange(ranges.get(0).start(), ranges.get(i)) ||
isDateInRange(ranges.get(0).end(), ranges.get(i))) {
overlaping = true;
break;
}
}
return overlaping || isThereOverlapingRanges(ranges.subList(1, ranges.size());
}
private boolean isDateInRange(DateTime date, CustomDateRange range) {
return date.after(range.start()) && date.before(range.end());
}

data structure is range or tuple [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to convert the following Java code into swift, but I do not know about this data structure new (int start, int end)[input. length], any guidance would be appraciated.
public override void collection_entries(int[] input)
{
var ranges = new (int start, int end)[input.length];
}
In swift, you should use a Range().
let start = 0
let end = 6
var ranges = Range(start...end)[input.count]

Stack forEach expression to loop for difference between existing size and new one [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a class with data member:
private static final int DEFAULT_SIZE = 10;
private Stack<String> myStack = new Stack<>();
// want to add more element to the stack based on demand - but add incremental demand
public void addCapacity(int incremental) {
int diff = incremental - DEFAULT_SIZE;
myStack.forEach((diff) -> {
myStack.push("something");
});
}
The idea was to see if this forms a use case for lambda functions. But it will not allow me as it is expecting a boolean in place of diff. Is lambda forEach a use-case here?
Since Stack extends Vector, if you wanted to increase the capacity, then you could have used ensureCapacity (in Vector)
myStack.ensureCapacity(minCapacity);
If you wanted to do myStack.push("something") diff times, then you could have used:
IntStream.range(0, diff).forEach(i -> myStack.push("something"));

For each Loop in Java 8 : What is the best way to write the following code in Java 8 [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
What is the best way to write the following code in Java 8 in terms of for loop and filtering.
boolean flag = true;
List<Feed> availableFeeds = data.getAvailableFeeds();
for (Feed feedElement : availableFeeds) {
String type = feedElement.getType();
if ("MatchState".equals(type)) {
flag = false;
break;
}
}
boolean flag = data.getAvailableFeeds()
.stream()
.map(Feed::getType)
.noneMatch("MatchState"::equals)
The first line creates a stream out of the list. The second one maps each Feed to type by calling getType. The last one returns true if there is no type that equals the string "MatchState".
You need filtering and a short break:
boolean flag = !availableFeeds.stream()
.map(Feed::getType)
.anyMatch(type -> "MatchState".equals(type));
or:
boolean flag = availableFeeds.stream()
.map(Feed::getType)
.allMatch(type -> !"MatchState".equals(type));
One way to do it is:
boolean flag = !data.getAvailableFeeds()
.stream()
.filter(Objects::nonNull)
.anyMatch(feed -> feed.getType().equals("MatchState"));
This example constructs a Stream<Feed> from the List<Feed> returned by getAvailableFeeds(), and then calls Stream<Feed>.anyMatch() which accepts a Predicate<Feed> as it's parameter, in this case the predicate is a feed where the feed is not null, and the feed's type returned by Feed.getType() equals "MatchState"

If statement based on what boolean method returns [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to use this boolean method in my app. I want to use what it returns in an if statement so if it returns true I can do something. Something like
if(isNook() == True) {
//do something
}
I'm sure the answer is obvious but I couldn't find anything on how to do this.
Why don't you use:
if (isNook()) {
// do something
}
People are suggesting this:
if (isNook()) {
// do something
}
which is correct. However, we need to discuss your attempt. This was it:
if(isNook() == True) {
// do something
}
In Java, boolean variables are represented with true and false - NOT True and False - the values are case sensitive.
To contrast, for example, Python uses the values True and False
Your attempt should have been this:
if(isNook() == true) {
// do something
}
Try this:
boolean bool_result;
if(bool_result = isNook( )) {
// do stuff with bool_result here
}
result can actually be other data types (i.e. int/char/string) as long as it evaluates to true or false.

Categories

Resources