开发者

how to check whether an element already exists in the array

开发者 https://www.devze.com 2023-01-06 15:55 出处:网络
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 valu

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


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

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


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...


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

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

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


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 ArrayList or Vector which has contains method with which you can compare any object.

0

精彩评论

暂无评论...
验证码 换一张
取 消