Home  >  Article  >  Java  >  Java Example - Find specified element in array

Java Example - Find specified element in array

黄舟
黄舟Original
2017-02-21 09:41:391945browse

The following example demonstrates how to use the contains () method to find the specified element in the array:

/*
 author by w3cschool.cc
 文件名:Main.java
 */import java.util.ArrayList;public class Main {
   public static void main(String[] args)  {
      ArrayList objArray = new ArrayList();
      ArrayList objArray2 = new ArrayList();
      objArray2.add(0,"common1");
      objArray2.add(1,"common2");
      objArray2.add(2,"notcommon");
      objArray2.add(3,"notcommon1");
      objArray.add(0,"common1");
      objArray.add(1,"common2");
      System.out.println("objArray 的数组元素:"+objArray);
      System.out.println("objArray2 的数组元素:"+objArray2);
      System.out.println("objArray 是否包含字符串common2? : "
      +objArray.contains("common1"));
      System.out.println("objArray2 是否包含数组 objArray? :"
      +objArray2.contains(objArray) );
   }}

The output result of running the above code is:

objArray 的数组元素:[common1, common2]
objArray2 的数组元素:[common1, common2, notcommon, notcommon1]
objArray 是否包含字符串common2? : true
objArray2 是否包含数组 objArray? :false

The above is Java Example - Find the content of a specified element in an array. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn