Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

compare an array with another array?

Fri Oct 24, 2008 6:12 pm

The program is basically taking 6 random numbers betweem 1 - 55
(RandPbNum[]) and comparing them to an array of arrays of the
PlayedPbNum[] numbers.

The problem is that I can't seem to get the RandPbNum[1] to compare to
the PlayedPbNums[1][1], looping through PlayedPbNums [1][2],[1][3]..
etc...
I have looked in many places for the answer and have tried coding it a
few different ways but I end up with a very laborious code that oesnt
bwork as I expect. There has to be a cleaner way than nesting if-else
statements on every element of the array?



Re: compare an array with another array?

Fri Oct 24, 2008 6:13 pm

Code:
int RandPbNum [] = ...;
int PlayedPbNum [][] = ...;
...
int i;
for (i = 0; i < 55; i++) {
if (RandPbNum [i] != PlayedPbNum [1][i]) {
break;
}
}
if (i == 55) {
System.out.println ("RandPbNum [] and PlayedPbNum [1][] are equal");
} else {
System.out.println ("RandPbNum [] and PlayedPbNum [1][] are unequal
at position " + i);
}


Of course, RandPbNum and PlayedPbNum could be a Vector and Vector [],
respectively, and you could use the native equals () comparison (which
does this code anyway). The downside to this is that you would have to
wrap everything in an Integer object. Messy. If I were you, I'd stick
with the above code.

Post a reply
  Related Posts  to : compare an array with another array?
 Array difference for associate array     -  
 Array C++ Class     -  
 Average of an array. Please help     -  
 PHP Array Functions     -  
 Array shuffle     -  
 Array Passing     -  
 Here is how to display any 2d array     -  
 Imploding an Array     -  
 array unshift     -  
 Array size to zero     -  

Topic Tags

Java Arrays