private static boolean moreThanOnce(ArrayList<Integer> list, int number) {
if (list.contains(number)) {
return true;
}
return false;
}
How do I use list.contains
to check if the number was found in the list more than once? I could make a method with for
loop, but I want to know if it's possible to do using .contains
. Thanks for help!
ArrayList list = new ArrayList<double>(1.38, 2.56, 4.3); The first code showed that the constructor ArrayList<Double>(double, double, double) is undefined and the second code shows that dimensions are required after double .
Use the wrapper class (ie, Double ) instead : public ArrayList<Double> list = new ArrayList<>(); Also, as of Java 7, no need to specify that it's for the Double class, it will figure it out automatically, so you can just specify <> for the ArrayList.
Look at this: ArrayList<Double> list = new ArrayList<Double>(); it creates a list that can only contain double values. A double is a variable type that can contain whole numbers and decimal numbers. So creating a list like this only allows the program to add numbers to the list: list.
contains() in Java. ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Returns: It returns true if the specified element is found in the list else it returns false.
I tried both The first code showed that the constructor ArrayList<Double> (double, double, double) is undefined and the second code shows that dimensions are required after double. which returns a fixed size list.
ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Syntax: public boolean contains(Object) object-element to be searched for
We can use the Object class to declare our ArrayList using the syntax mentioned below. ArrayList<Object> list = new ArrayList<Object> (); The above list can hold values of any type. The code given below presents an example of the ArrayList with the Objects of multiple types.
The following article provides an outline for 2D ArrayList in Java. In java array list can be two dimensional, three dimensional etc. The basic format of the array list is being one dimensional. Apart from one dimensional all other formats are considered to be the multi-dimensional ways of declaring arrays in java.
You can't do it just with contains
. That just tests if the item is anywhere in the list.
You can do it with indexOf
, though. There are two overloads of indexOf
, one of which allows you to set a position in the list to start searching from. So: after you find one, start from one after that position:
int pos = list.indexOf(number);
if (pos < 0) return false;
return list.indexOf(number, pos + 1) >= 0;
Or replace the last line with:
return list.lastIndexOf(number) != pos;
If you want a more concise way (although this iterates the whole list twice in the case that it's not found once):
return list.indexOf(number) != list.lastIndexOf(number);
You can use Stream
s:
private static boolean moreThanOnce(ArrayList<Integer> list, int number) {
return list.stream()
.filter(i -> i.equals (number))
.limit(2) // this guarantees that you would stop iterating over the
// elements of the Stream once you find more than one element
// equal to number
.count() > 1;
}
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