Java 如何检查一个元素是否已经存在于数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3204610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:23:41  来源:igfitidea点击:

how to check whether an element already exists in the array

javablackberry

提问by Arunabha Dutta Choudhury

i am sending value from one page to another,i want to store the values which i hav sent,& then want to compare the value which i am sending with the one which i hav already sent,i.e the saved value.while storing the values into an arrayin persistent object,& then comparing the value with another array,i face some prob,can anyone tell me how to check whether a value already exits in the array,i am giving the code,kindly help

我正在将值从一页发送到另一页,我想存储我发送的值,然后想将我发送的值与我已经发送的值进行比较,即保存的值。同时存储值进入持久对象的数组,然后将值与另一个数组进行比较,我面临一些问题,谁能告诉我如何检查数组中是否已经存在值,我正在提供代码,请帮忙

package com.firstBooks.series.db;

import java.util.Random;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.util.Arrays;
import net.rim.device.api.util.Persistable;
import com.firstBooks.series.db.parser.XMLParser;
import com.firstBooks.series.ui.managers.TopManager;
import com.firstBooks.series.ui.screens.TestScreen;
public class DBMain implements Persistable{

    public static String answer = "";
    public static String selectedAnswer = "";
    public static Question curQuestion;

    public static int currQuesNumber = 1;
    public static int correctAnswerCount = -1;
    static int curQuesnew;
    static int quesCount=-1;
    static int xyz;
    static int j=0;
    public static int totalNumofQuestions = Question.totques;
    public static int quesNum[] = new int[XMLParser.questionList.size()];
    static PersistentObject qStore;
    static PersistentObject curQues;
    static PersistentObject pQues;
    static PersistentObject curans;
    static PersistentObject disques;
    static PersistentObject restques;
    static int a ;
    static int b;
    static int[] c ;
    static int[] d ;
    static int pques;
    static int cans;
    static int[] dques;
    static int[] rques;

    static {
        qStore = PersistentStore.getPersistentObject(0x33010065d24c7883L);
        curQues = PersistentStore.getPersistentObject(0x33010064d24c7883L);
        pQues   = PersistentStore.getPersistentObject(0xbd7460a5b4f9890aL);
        curans  = PersistentStore.getPersistentObject(0xc5c065a3ae1bec21L);
        disques = PersistentStore.getPersistentObject(0xbf8118045165a07aL);
    }

    static{ 
         initialize();
    }


    public static void initialize() {

        //int quesNum[] = new int[XMLParser.questionList.size()];

        Random rgen = new Random(); // Random number generator
        //int a=Integer.parseInt(TopManager.quesNumber);
        //System.out.println("The value of AAAAAAA is...."+a);
        // --- Initialize the array
        for (int i = 0; i < quesNum.length; i++) {
            quesNum[i] = i;
        }

        // --- Shuffle by exchanging each element randomly
        for (int i=0; i< quesNum.length; i++) {
            int randomPosition = rgen.nextInt(quesNum.length);
            int temp = quesNum[i];
            quesNum[i] = quesNum[randomPosition];
            quesNum[randomPosition] = temp;

        }

            synchronized (qStore) {

            qStore.setContents(quesNum);
        qStore.commit();
        }
        }


    public static int getQuestionNumber() {


        //int quesCount;
           //quesCount++;
        //synchronized (curQues) {
            //quesCount = Integer.parseInt((String)curQues.getContents());
        //}
            quesCount++;
           synchronized (curans) {

                int b=Integer.parseInt(TopManager.corrCount);
                curans.setContents(b+"");
                curans.commit();

            }
    //int quesNum[];

        synchronized (qStore) {
            quesNum=(int[]) qStore.getContents();
            System.out.println("The value of question is ...."+quesNum.length);
    }


        synchronized (pQues) {

            int a=Integer.parseInt(TopManager.quesNumber);
            pQues.setContents(a+"");
            pQues.commit();

        }

        if (quesNum!=null && quesCount < quesNum.length) {

            synchronized (curQues) {

                curQuesnew=quesNum[quesCount];
                curQues.setContents(curQuesnew+"");
                curQues.commit();   
            }

            synchronized (disques) {
             c[j]=TestScreen.quesNumber;
             System.out.println("Astala vistaaaaaaaaaaaaa"+c);
                disques.setContents(c+"");
                disques.commit();
              }
            synchronized (disques) {
                dques[j]=Integer.parseInt((String)disques.getContents());
                System.out.println("valueee is.........."+dques);
             }

        for(int k=0;k<dques.length;k++){
         if(quesNum[quesCount]!=dques[k]){
         System.out.println("ghijyghfhgfhfhgfhgfhgfhgfhgddkjklmn");
          xyz=quesNum[quesCount];
         }
         j++;
        }


         return xyz;


    } else {
            initialize();
            quesCount = -1;
            return getQuestionNumber();
        }

    }


    public  static int getpresentQuestionNumber(){


        synchronized (pQues) {
            pques=Integer.parseInt((String)pQues.getContents());

    }
        return pques;
 }

  public  static int getCorrectanswerNumber(){
     synchronized (curans) {
            cans=Integer.parseInt((String)curans.getContents());
            }
        return cans;
 }



  public static int getCurrQuestionNumber() {
        synchronized (curQues) {
            return Integer.parseInt((String)curQues.getContents());
            //return curQuesnew;
            //curQuesnew=(int[]) curQues.getContents();
            //return curQuesnew[quesCount]; 
        }
    }
}

回答by Riduidel

I think Arrays.binarySearchmay help you find if array contains the given comparable element.

我认为Arrays.binarySearch可以帮助您查找数组是否包含给定的可比较元素。

However, you should consider replacing your array with one of the various Java collections.

但是,您应该考虑用各种 Java 集合之一替换您的数组。

According to Michael comment, Snoracle doc was not correct. i've changed link destination to Blackberry documentation. Thanks Michael.

根据迈克尔的评论,Snoracle 文档不正确。我已将链接目的地更改为 Blackberry 文档。谢谢迈克尔。

回答by Curtis

Do you really have to use an Array? If you really are going to check to see if a value already exists a lot, you're probably better off with a structure that makes this faster - HashSet, TreeSet, HashMap, etc...

你真的必须使用数组吗?如果你真的要检查一个值是否已经存在很多,你可能最好使用一个更快的结构 - HashSet、TreeSet、HashMap 等......

回答by Venkat

You could write like this :

你可以这样写:

   for(int i=0; i< array.length; i++)  {
     if(value.equals(array[i])) {
       return true;
     }
   }

   return false;

But There's an alternative way you can do this with ArrayListor Vectorwhich has containsmethod with which you can compare any object.

但是,您可以使用另一种方法来执行此操作,ArrayList或者Vector它具有contains可以比较任何对象的方法。

回答by xagyg

One way to check if a value exists in an array:

检查数组中是否存在值的一种方法:

Arrays.asList(someArray).contains(value);