Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList, double contains

Tags:

java

arraylist

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!

like image 939
Lukas Avatar asked Apr 23 '20 09:04

Lukas


People also ask

Can we use double in ArrayList?

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 .

How do you make an ArrayList of doubles?

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.

What does ArrayList double mean?

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.

Can we use contains in ArrayList?

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.

Is there a way to define an ArrayList with the double type?

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.

What is ArrayList contains () method in Java?

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

How to declare ArrayList with multiple types in Java?

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.

What is 2D ArrayList in Java?

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.


2 Answers

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);
like image 104
Andy Turner Avatar answered Sep 27 '22 20:09

Andy Turner


You can use Streams:

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;
}
like image 41
Eran Avatar answered Sep 27 '22 19:09

Eran