i need to find a integer data in arraylist?

后端 未结 4 777
抹茶落季
抹茶落季 2020-12-16 14:26

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 hop

4条回答
  •  孤街浪徒
    2020-12-16 14:46

    If I understand your question, you want to check if an array list already contains and integer value. If so, you could use ArrayList.contains().

    Sample code here:

    ArrayList list = new ArrayList();
    int x = 4, y = 7;
    int z = x;
    
    list.add(x);
    
    //List contains the value 4, which is the value stored in z
    //Program will output "List contains 4"
    if(list.contains(z))
    {
        System.out.printf("List contains %d\n", z);
    }
    else
    {
        System.out.printf("List does not contain %d\n", z);
    }
    
    //List contains the value 7, which is the value stored in y
    //Program will output "List does not contain 7"   
    if(list.contains(y))
    {
        System.out.printf("List contains %d\n", y);
    }
    else
    {
        System.out.printf("List does not contain %d\n", y);
    }
    

提交回复
热议问题