如何检查数组中是否已存在元素

时间:2010-07-08 14:26:04

标签: java blackberry

我正在从一个页面向另一个页面发送价值,我想存储我发送的值,&那么我想比较我发送的值和我已发送的值,即保存的值。同时将值存储到持久对象的数组中,&然后比较该值与另一个数组,我面临一些问题,任何人都可以告诉我如何检查一个值是否已经退出数组,我给的代码,请帮助

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]; 
        }
    }
}

4 个答案:

答案 0 :(得分:10)

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

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

答案 1 :(得分:2)

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

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

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

答案 2 :(得分:2)

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

答案 3 :(得分:1)

你可以这样写:

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

   return false;

但是,使用 ArrayList Vector contains 用于比较任何对象的方法。