Hi! I'm looking to learn something today, and I have just the question.
In writing a recent program, I found no Java method suited to compare two byte arrays. Maybe the method exists, or maybe not. In any case, this is a learning expedition, and I came up with a simple method designed to compare arrays of values. SontEqual can be adapted to byte, short, char, int, long, etc, and deals with arrays of such values.
static boolean SontEqual (byte[] a, byte[] b) {
// Compare two byte arrays
// Method by Matthew Cudmore ^.^
// ----------
if (a == null || b == null) return (a == b);
if (a.length != b.length) return false;
// ----------
for (int i = 0; i < a.length; i++)
if (a[i] != b[i]) return false;
// ----------
return true;
}
Now there is a question in mind: can a single method be adapted to represent all numerical primitive types? Or must a separate method be written for each primitive type of array? I'm sure I've seen something of the <?> nature (or whatever it was), and I think it can be done.. Maybe it's got something to do with C++ and not Java. In any case, thanks in advance! I'm about to research this myself, and will post any significant findings for validation. ;)