[Java] How to search for a value in an array (or list) with the contains method

Programming study diary

November 6, 2020 When dealing with arrays in Java, I had a hard time searching for values from the elements of the array, so I will summarize them.

How to search an array

There is a way to use the contains method when searching for a specific value in an array. You need to import hava.util.Arrays to use the contains method. Enter the value you want to search in the argument of contains, and if it exists, it will be true and the process within the condition will be passed.

Search for numbers


import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    Integer num[] = {10, 20, 30};
    //Whether the array contains 30
    if(Arrays.asList(num).contains(30)) {
      System.out.println("Value exists");
    }
  }
}

Execution result


Value exists

Search for strings


import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    String bar[] = {"aa", "bb", "cc"};
    //Whether the array contains aa
    if(Arrays.asList(bar).contains("aa")) {
      System.out.println("Value exists");
    }
  }
}

Execution result


Value exists

How to search the List

You need to import java.util.ArrayList. The basic writing style is the same as an array.

import java.util.ArrayList;
import java.util.List;
public class Main {
  public static void main(String[] args) {
    List<Integer> num = new ArrayList<Integer>();
    num.add(10);
    num.add(20);
    num.add(30);
        
    if(num.contains(10)) {
      System.out.println("Value exists");
    }
  }
}

References

[Java] Summary of how to search array and List values with contains method Judge a specific value! How to use Java contains method [for beginners]

Recommended Posts

[Java] How to search for a value in an array (or list) with the contains method
How to make a judgment method to search for an arbitrary character in an array
[Java] How to turn a two-dimensional array with an extended for statement
How to retrieve the hash value in an array in Ruby
How to save a file with the specified extension under the directory specified in Java to the list
How to output the value when there is an array in the array
Cast an array of Strings to a List of Integers in Java
How to specify an array in the return value / argument of the method in the CORBA IDL file
I want to ForEach an array with a Lambda expression in Java
[Java] [For beginners] How to insert elements directly in a 2D array
How to use an array for a TreeMap key
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
(Java) How to implement equals () for a class with value elements added by inheritance
How to use a structure with variable length array or bit field in Ruby-FFI
How to get the class name / method name running in Java
How to change a string in an array to a number in Ruby
Create a method to return the tax rate in Java
How to test a private method with RSpec for yourself
How to add the same Indexes in a nested array
Mapping to a class with a value object in How to MyBatis
For Java beginners: List, Map, Iterator / Array ... How to convert?
How to make a Java array
[Ruby] How to count even or odd numbers in an array
Sorting a list with an int array as an element (Java) (Comparator)
Connecting to a database with Java (Part 1) Maybe the basic method
Replace with a value according to the match with a Java regular expression
How to store the information entered in textarea in a variable in the method
How to check for the contents of a java fixed-length string
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
About the method to convert a character string to an integer / decimal number (cast data) in Java
How to change arguments in [Java] method (for those who are confused by passing by value, passing by reference, passing by reference)
How to deal with the type that I thought about writing a Java program for 2 years
[Java] How to compare with equals method
[Java] How to use the toString () method
How to get the date in java
How to test a private method in Java and partially mock that method
Declare a method that has a Java return value with the return value data type
[Personal memo] How to interact with a random number generator in Java
How to convert an array of Strings to an array of objects with the Stream API
[Java small story] Monitor when a value is added to the List
How to get the value after "_" in Windows batch like Java -version
How to change the value of a variable at a breakpoint in intelliJ
How to get the absolute path of a directory running in Java
How to make an app with a plugin mechanism [C # and Java]
How to reference a column when overriding the column name method in ActiveRecord
How to create your own annotation in Java and get the value
[Java] How to test for null with JUnit
How to display a web page in Java
How to use an array for HashMap keys
How to create pagination for a "kaminari" array
[Java] (for MacOS) How to set the classpath
[Java] Get a random value from an array
How to use the replace () method (Java Silver)
How to solve an Expression Problem in Java
[Swift] How to get the number of elements in an array (super basic)
[Java] How to convert one element of a String type array to an Int type
If the parameter is an array, how to include it in Stopara's params.permit
[Java] Various methods to acquire the value stored in List by iterative processing