| | |
Super-power compare method? <?>
![]() |
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.
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.
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.
java Syntax (Toggle Plain Text)
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.
Last edited by Cudmore; Feb 2nd, 2007 at 11:41 am. Reason: Spell checking
synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
How many people have code in their Sigs?
first, never name methods starting with a capital letter. That should ONLY be done with classes (by convention, and a good one it is).
As to your question. For primitive types it's rather impossible because of the way generics in Java work (they work only with classes and interfaces).
For those though it's rather sillily easy (took me a while to get it to compile because of a stupid typo I didn't catch in the compiler errors, but the code itself is trivial).
I omitted part of your checks for brevity.
As to your question. For primitive types it's rather impossible because of the way generics in Java work (they work only with classes and interfaces).
For those though it's rather sillily easy (took me a while to get it to compile because of a stupid typo I didn't catch in the compiler errors, but the code itself is trivial).
Java Syntax (Toggle Plain Text)
class SomeClass<T> { public <T> boolean compare(T[] a, T[] b) { for (int i = 0; i < a.length; i++) { if (!a[i].equals(b[i])) { return false; } } return true; } }
I omitted part of your checks for brevity.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
:o I noticed that little capital after posting.. Just didn't change it. Please excuse any naive stylistic choices, as I'm not yet completely accustomed to "convention".
And I did stumble upon the Java lesson about Generics here:
http://java.sun.com/docs/books/tutor...ics/index.html
No idea how I found it, because I didn't know what to look for, but now I think I understand this.
Generics work well in source code. However, "type erasure" occurs when compiled, so even if you were to work with Objects and not primitives, you couldn't have a single compiled method represent all possible class types. For deployed libraries, the source would have to be deployed (which is fine) and the generics would be worked out during compilation.
Kay, so I've learned: It's not possible for a single sontEqual method to represent all numerical primitive types. The method would need to be overridden for each supported type if it were to be deployed in a compiled library.
And I did stumble upon the Java lesson about Generics here:
http://java.sun.com/docs/books/tutor...ics/index.html
No idea how I found it, because I didn't know what to look for, but now I think I understand this.
Generics work well in source code. However, "type erasure" occurs when compiled, so even if you were to work with Objects and not primitives, you couldn't have a single compiled method represent all possible class types. For deployed libraries, the source would have to be deployed (which is fine) and the generics would be worked out during compilation.
Kay, so I've learned: It's not possible for a single sontEqual method to represent all numerical primitive types. The method would need to be overridden for each supported type if it were to be deployed in a compiled library.
Last edited by Cudmore; Feb 2nd, 2007 at 4:43 pm. Reason: trying to be perfect
synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
How many people have code in their Sigs?
Yes, the binding is determined at compile time. But that hardly matters in practice, unless you're trying to write a program that's so generic that you don't know even a common basetype at that moment and have no choice but to pass in Object everywhere (at which point generics no longer make sense and you could just as well use Object as the parameter type everywhere).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Similar Threads
- What super power would you most likely want? (Geeks' Lounge)
Other Threads in the Java Forum
- Previous Thread: Help with Censor.
- Next Thread: MySql connection question.
| Thread Tools | Search this Thread |
911 actionlistener addball addressbook android api applet application array automation binary block bluetooth button character class client code component consumer css csv database desktop developmenthelp eclipse ee error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia jvm lego linked linux loan mac map method mobile netbeans notdisplaying number objects online oriented phone printf problem program programming project projects recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set singleton sms software sort sql swing system test textfields threads time title tree tutorial-sample ubuntu update windows working






