如何检查一个元素是否已经存在阵列中的 [英] how to check whether an element already exists in the array

查看:122
本文介绍了如何检查一个元素是否已经存在阵列中的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从一个页面发送值到另一个,我想保存我甲肝发送的值,&安培;然后要比较哪个我跟我甲肝已经发出一个发送的值,即保存value.while存储值到arrayin持久化对象,&安培;然后用另一个阵列比较值,我面对一些概率,谁能告诉我如何检查值是否已经存在数组中,我给了code,好心帮

 包com.firstBooks.series.db;进口了java.util.Random;进口net.rim.device.api.system.PersistentObject;
进口net.rim.device.api.system.PersistentStore;
进口net.rim.device.api.util.Arrays;
进口net.rim.device.api.util.Persistable;
进口com.firstBooks.series.db.parser.XMLParser;
进口com.firstBooks.series.ui.managers.TopManager;
进口com.firstBooks.series.ui.screens.TestScreen;
公共类DBMain实现持久化{    公共静态字符串的回答=;
    公共静态字符串selectedAnswer =;
    公共静态问题curQuestion;    公共静态INT currQuesNumber = 1;
    公共静态INT correctAnswerCount = -1;
    静态INT curQuesnew;
    静态INT quesCount = -1;
    静态INT XYZ;
    静态INT J = 0;
    公共静态INT totalNumofQuestions = Question.totques;
    公共静态INT quesNum [] =新的INT [XMLParser.questionList.size()];
    静态持久性对象qStore;
    静态持久性对象curQues;
    静态持久性对象pQues;
    静态持久性对象curans;
    静态持久性对象DISQUES;
    静态持久性对象restques;
    静态int类型的;
    静态INT B:
    静态INT [℃;
    静态INT [] D;
    静态INT pques;
    静态INT罐;
    静态INT [] dques;
    静态INT [] rques;    静态的 {
        qStore = PersistentStore.getPersistentObject(0x33010065d24c7883L);
        curQues = PersistentStore.getPersistentObject(0x33010064d24c7883L);
        pQues = PersistentStore.getPersistentObject(0xbd7460a5b4f9890aL);
        curans = PersistentStore.getPersistentObject(0xc5c065a3ae1bec21L);
        DISQUES = PersistentStore.getPersistentObject(0xbf8118045165a07aL);
    }    静态的{
         初始化();
    }
    公共静态无效的initialize(){        // INT quesNum [] =新的INT [XMLParser.questionList.size()];        随机RGen元=新的随机(); //随机数发生器
        // int类型的=的Integer.parseInt(TopManager.quesNumber);
        AAAAAAA的//System.out.println(\"The值是......+一);
        // ---初始化数组
        的for(int i = 0; I< quesNum.length;我++){
            quesNum [我] =我;
        }        // ---每个元素随机交换洗牌
        的for(int i = 0; I< quesNum.length;我++){
            INT randomPosition = rgen.nextInt(quesNum.length);
            INT TEMP = quesNum [I]
            quesNum [I] = quesNum [randomPosition]
            quesNum [randomPosition] =温度;        }            同步(qStore){            qStore.setContents(quesNum);
        qStore.commit();
        }
        }
    公共静态INT getQuestionNumber(){
        // INT quesCount;
           // quesCount ++;
        //同步(curQues){
            // quesCount =的Integer.parseInt((字符串)curQues.getContents());
        //}
            quesCount ++;
           同步(curans){                INT B =的Integer.parseInt(TopManager.corrCount);
                curans.setContents(B +);
                curans.commit();            }
    // INT quesNum [];        同步(qStore){
            quesNum =(INT [])qStore.getContents();
            的System.out.println(问题的价值......+ quesNum.length);
    }
        同步(pQues){            int类型的=的Integer.parseInt(TopManager.quesNumber);
            pQues.setContents(A +);
            pQues.commit();        }        如果(quesNum = NULL&放大器;!&安培; quesCount< quesNum.length){            同步(curQues){                curQuesnew = quesNum [quesCount]
                curQues.setContents(curQuesnew +);
                curQues.commit();
            }            同步(DISQUES){
             C [J] = TestScreen.quesNumber;
             的System.out.println(Astala vistaaaaaaaaaaaaa+ C);
                disques.setContents(C +);
                disques.commit();
              }
            同步(DISQUES){
                dques [J] =的Integer.parseInt((字符串)disques.getContents());
                的System.out.println(valueee是..........+ dques);
             }        对于(INT K = 0; K< dques.length; k ++){
         如果(quesNum [quesCount]!= dques [K]){
         的System.out.println(ghijyghfhgfhfhgfhgfhgfhgfhgddkjklmn);
          XYZ = quesNum [quesCount]
         }
         J ++;
        }
         返回XYZ;
    }其他{
            初始化();
            quesCount = -1;
            返回getQuestionNumber();
        }    }
    公共静态INT的get presentQuestionNumber(){
        同步(pQues){
            pques =的Integer.parseInt((字符串)pQues.getContents());    }
        返回pques;
 }  公共静态INT getCorrectanswerNumber(){
     同步(curans){
            罐=的Integer.parseInt((字符串)curans.getContents());
            }
        返回罐;
 }  公共静态INT getCurrQuestionNumber(){
        同步(curQues){
            返回的Integer.parseInt((字符串)curQues.getContents());
            //返回curQuesnew;
            // curQuesnew =(INT [])curQues.getContents();
            //返回curQuesnew [quesCount]
        }
    }
}


解决方案

要检查的一种方式,如果在数组中存在的值:

  Arrays.asList(的someArray)。载有(值);

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);

这篇关于如何检查一个元素是否已经存在阵列中的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆