For example:
if (value == array[size]) //if the value (unique) is present in an array then do something
can this be done in one statement without having to call a function or a basic for loop statement?
Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.
A simple way is to run a loop and compare elements one by one. Java provides a direct method Arrays. equals() to compare two arrays. Actually, there is a list of equals() methods in the Arrays class for different primitive types (int, char, ..etc) and one for Object type (which is the base of all classes in Java).
equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.
std::find
can do it in one statement, but it's not as trivial as other languages :(
int array[10];
if (array + 10 != find(array, array + 10, 7)) {
cout << "Array contains 7!";
}
Or with std::count
:
if (int n = count(array, array + 10, 7)) {
cout << "Array contains " << n << " 7s!";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With