java - How can I ensure a value is contained within an array? -


the following code :

it testing prime number generator creates , populates arraylist first n prime numbers. in test, create array of known prime numbers, , use method construct arraylist of first 50 (knownprimes.length) prime numbers. random number picked, , asserttrue every prime number picked using method nextrandomprime (which picks number arraylist of unknown/generated primes) contained within array knownprimes. how can ?

in psuedocode :

asserttrue(createdprimelist.nextrandomprime value in array knownprimes); 

here have gotten far :

 public void compareprimes() {     int[] knownprimes = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,        31,  37,  41,  43,  47,  53,  59,  61,  67,  71,        73,  79,  83,  89,  97, 101, 103, 107, 109, 113,        127, 131, 137, 139, 149, 151, 157, 163, 167, 173,        179, 181, 191, 193, 197, 199, 211, 223, 227, 229 };      primes createdprimelist = new primes (knownprimes.length);     (int index = 0; index < nooftests; index++) //nooftests global variable     {       asserttrue( createdprimelist.nextrandomprime( ) in knownprimes );//line struggling     }   } 

would please able me out ?

thanks lot.

use binary search. can use arrays.binarysearch(int[] array, int key).

in order verify whether next value stored in knownprimes should this:

int nextvalue = createdprimelist.nextrandomprime(); if (arrays.binarysearch(knownprimes, nextvalue) >= 0) {     system.out.println("the value stored in known primes"); } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -