Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i need to find a integer data in arraylist?

Tags:

java

arraylist

I have an Arraylist. If user enter the same number secondly I want to show to user. For this I need to find Arraylist have it or not.

I hope I made myself clear.

like image 472
Joseph Avatar asked Feb 18 '12 16:02

Joseph


People also ask

How do you search a number in an ArrayList?

An element in an ArrayList can be searched using the method java. util. ArrayList. indexOf().

How do you check if an ArrayList contains an Integer?

If you are checking to see if some value is stored in an ArrayList you can use the contains() method, this will return true if the object is in the list, false otherwise.

Can you use int in ArrayList?

ArrayList can not be used for primitive types, like int, char, etc.

How do you access data from an ArrayList?

An element can be retrieved from the ArrayList in Java by using the java. util. ArrayList. get() method.


2 Answers

If you are checking to see if some value is stored in an ArrayList you can use the contains() method, this will return true if the object is in the list, false otherwise.

ArrayList<Integer> intList = new ArrayList<>();

intList.add(5);
intList.add(7);
intList.add(3);
intList.add(-2);

intList.contains(-1); //returns false
intList.contains(3); //returns true
like image 103
Hunter McMillen Avatar answered Oct 26 '22 23:10

Hunter McMillen


You might want to use ArrayList.contains() to check if the element is in the ArrayList or not.

like image 33
amit Avatar answered Oct 26 '22 22:10

amit