009:How can I test if an array contains a certain value?

Posted 氵冫丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了009:How can I test if an array contains a certain value?相关的知识,希望对你有一定的参考价值。

题目:判断一个元素是否在数组中?

1.存储到List中,让List 去遍历
2.存储到Set中,让set去遍历
3.自己顺序遍历
4.二分查找

import java.util.*;



public class stackoverFlow 

    public static void main(String[] args) 
        String[] A = new String[]"AA","BB","CC","DD","EE";
        int limit = 10000000;
        String target = "CC";
        //use list
        long startTime = System.nanoTime();
        for (int i = 0; i < limit; i++) 
            useList(A, target);
        
        long endTime = System.nanoTime();
        long duration = endTime - startTime;
        System.out.println("useList:  " + duration / 1000000);

        //use set
        startTime = System.nanoTime();
        for (int i = 0; i < limit; i++) 
            useSet(A, target);
        
        endTime = System.nanoTime();
        duration = endTime - startTime;
        System.out.println("useSet:  " + duration / 1000000);

        //use loop
        startTime = System.nanoTime();
        for (int i = 0; i < limit; i++) 
            useLoop(A, target);
        
        endTime = System.nanoTime();
        duration = endTime - startTime;
        System.out.println("useLoop:  " + duration / 1000000);

        //use Arrays.binarySearch()
        startTime = System.nanoTime();
        for (int i = 0; i < limit; i++) 
            useArraysBinarySearch(A, target);
        
        endTime = System.nanoTime();
        duration = endTime - startTime;
        System.out.println("useArrayBinary:  " + duration / 1000000);


    
    /**
     * List
     * @param A
     * @param target
     * @return
     */
    private static boolean useList(String[] A,String target)
        return Arrays.asList(A).contains(target);
    

    /**
     * Set
     * @param A
     * @param target
     * @return
     */
    private static boolean useSet(String[] A,String target)
        Set<String> set = new HashSet<String>(Arrays.asList(A));
        return set.contains(target);
    

    /**
     * Loop
     * @param A
     * @param target
     * @return
     */
    public static boolean useLoop(String[] A, String target) 
        for(String s: A)
            if(s.equals(target))
                return true;
        
        return false;
    

    /**
     * BinarySearch
     * @param A
     * @param target
     * @return
     */
    public static boolean useArraysBinarySearch(String[] A, String target)     
        int a =  Arrays.binarySearch(A, target);
        if(a > 0)
            return true;
        else
            return false;
    




输出
limit = 100000

useList:  13
useSet:  74
useLoop:  10
useArrayBinary:  4

limit = 10000000

useList:  118
useSet:  1778
useLoop:  106
useArrayBinary:  33

所以,普通的顺序遍历瞧过还是很好的,如果知道是排序的时候可以利用二分查找,或者利用红黑树进行查找,红黑树查找效率更高
参考:http://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/

本专题来源stackoverflow 标签是java的投票数比较高的问题以及回答,我只对上面的回答根据自己的理解做下总结。

以上是关于009:How can I test if an array contains a certain value?的主要内容,如果未能解决你的问题,请参考以下文章

You can add an index on a column that can have NULL values if you are using the MyISAM, InnoDB, or M

sh 使用tar在linux上压缩和解压缩文件夹 - 来自http://unix.stackexchange.com/questions/93139/can-i-zip-an-entire-folde

Git does not apply deleted files when merging an old branch into the master. How can I tell Git to a

Luogu1280 尼克的任务

464 Can I Win 我能赢吗

线性代数