I'm tring to compare the length of the two arrays. Plus catch the following two ClassCastException and NullPointerException;
Below is what I have currently I don't know if the try catch will work.
thanks for anything you can do to make this
work better.
**************************************
public int compareTo (Object ob)
{
double diff = this.doubleValue()-((UltraLong)ob).doubleValue();
{
try
{
NullPointerException ();
}
catch (ClassCastException exception)
{
System.out.println ("Object is not same type:");
}
}
return diff > 0 ? -1 : 1;
}//====================
public Exception NullPointerException ()
{
NullPointerException NPE= new NullPointerException("Answer is Null:");
return NPE;
}
Actually, I think it's supposed to be both lengths and the values.
Jesus! Working with arrays is one of the simplest things you can do regardless what programming language you use. Aren't you making fun?
Just run this test:
- if the length of arrays: if differs, return false
- only if the lengths are the same loop through arrays (with one index) and check array1[index] == array2[index] (for arrays of integers, for arrays of objects you will probably need to redefine the method equals) and you will either reach the end (then and only then are the arrays equal) or find the first difference and then they are different.
This will answer a question: the same x different
If you want to answer a question array1 < = > array2, you will first have to decide for ordering criteria. The easiest way is lexicographical order (like in a dictionary).
In this case your primary criteria cannot be the length. You just start looping through arrays comparing relevant members.
If array1[index] == array2[index] and arrays have the same length -> equal
If array1[index] == array2[index], but array1 is shorter than array2: array1 < array2 (and vice versa)
Otherwise, you will find first such index, where either
array1[index] < array2[index], or array1[index] > array2[index]. This will be your relationship between arrays.
For further details on arrays: http://java.sun.com/docs/books/tutorial/reflect/array/index.html
Maybe one more remark: the only untrivial problem is how to find the length of an array if you happen not to know it. Take a look at this code: import java.lang.reflect.*;
class SampleGetArray {
public static void main(String[] args) {
int[] sourceInts = {12, 78};
int[] destInts = new int[2];
copyArray(sourceInts, destInts);
String[] sourceStrgs = {"Hello ", "there ", "everybody"};
String[] destStrgs = new String[3];
copyArray(sourceStrgs, destStrgs);
}
public static void copyArray(Object source, Object dest) {
for (int i = 0; i < Array.getLength(source); i++) {
Array.set(dest, i, Array.get(source, i));
System.out.println(Array.get(dest, i));
}
}
}
(available at: http://java.sun.com/docs/books/tutorial/reflect/array/getset.html) which uses reflection API to query the length (Array.getLength(source)) of an array (source refers to the array sourceInts)
//Maybe one more remark: the only untrivial problem is how to find the length of an array if you happen not to know it
In java a field variable called 'length' is provided in arrays. A VERY typical example:
Object[] myArray = new Object[10];
for(int i = 0; i < myArray.length; i++)
{
//Bla bla bla
}
Its not harder than that..
i agree with nille ( unless i have misunderstood )
to compare the lengths of two arrays you can do:
e.g.,
int lengthOfttt = ttt.length;
int lengthOfppp = ppp.length;
you could then compare these two lengths in any way you liked:
e.g.,
if( lengthOfttt == lengthOfppp )
--do this--
else
--do this--
however, if you simply want to compare whether the two arrays are
equal, you can use:
boolean rTheyEqual = false;
rTheyEqual = Arrays.equals( ppp, ttt );
if the arrays a equal, rTheyEqual will be true, else false.
as far as the rest of the question, that is not my forte...
best of luck
Hey,
Don't you worry about performance? Did you know how slow are native calls to Reflect API? Man, do not use if possible. To copy array try this:
String
arr1 = {"a", "b"},
arr2 = new String[2];
copy(arr1, arr2, arr1,length);
static void copy(Object src, Object dst, int count) {
System.arraycopy(src, 0, dst, 0, count);
}
This could be hundreds times quicker, espetially for long arrays (more then 100 elements)... In case you use relative small arrays (about 10-20 elements) use your own copy implementations like:
String
arr1 = {"a", "b"},
arr2 = new String[2];
copy(arr1, arr2, arr1,length);
static void copy(String[] src, String[] dst, int count) {
for(int i=count; i-->0; )
dst[i] = src[i];
}
static void copy(int[] src, int[] dst, int count) {
for(int i=count; i-->0; )
dst[i] = src[i];
}
This code is fastest way to do it in looks quite proffesional. Each element copiing is even faster then system copiing for small arrays (up to 10 times!)... So before to try tp do something, check the cost you'll pay for it... Do you really need generalization for just 2 types (String and int) in cost of performance?
Good luck man,
Alex.
> Actually, the reflection api is pretty fast since
> JDK1.4.0...
No. Reflection could not be fast. In J2SDK1.4 it seems to be faster then in previous releases. But improvments do not include array operations. So here are some results of simple benchmark:
bash-2.05a$ /usr/java/jdk1.3.1_04/bin/java TestArray
Result for reflect copiing (3 elements, 1000000 times): 4915ms
Result for system copiing (3 elements, 1000000 times): 460ms
Result for pure java copiing (3 elements, 1000000 times): 137ms
bash-2.05a$
bash-2.05a$ /usr/java/j2sdk1.4.0_01/bin/java TestArray
Result for reflect copiing (3 elements, 1000000 times): 6082ms
Result for system copiing (3 elements, 1000000 times): 491ms
Result for pure java copiing (3 elements, 1000000 times): 115ms
bash-2.05a$
bash-2.05a$ /usr/java/j2sdk1.4.1/bin/java TestArray
Result for reflect copiing (3 elements, 1000000 times): 5738ms
Result for system copiing (3 elements, 1000000 times): 497ms
Result for pure java copiing (3 elements, 1000000 times): 157ms
bash-2.05a$
As you can see, in new Java reflect operations on arrays are even slower then in jdk1.3.1. System copiing is good alternative for pure java (element by element) copiing, espetially for big arrays. And here is the code of this benchmark, test it on your PC also:
import java.lang.reflect.*;
public class TestArray {
static void doNothing(){}
static int field;
public static void main(String[] args) throws Exception {
final int COUNT = 3, TIMES = 1000*1000;
final String[] arr1 = new String[COUNT];
final String[] arr2 = new String[COUNT];
{
long time = -System.currentTimeMillis();
for(int i=TIMES; i-->0;)
copyReflect(arr1, arr2, COUNT);
time += System.currentTimeMillis();
System.out.println("Result for reflect copiing ("+COUNT+" elements, "+TIMES+" times): "+time+"ms");
}
{
long time = -System.currentTimeMillis();
for(int i=TIMES; i-->0;)
copySystem(arr1, arr2, COUNT);
time += System.currentTimeMillis();
System.out.println("Result for system copiing ("+COUNT+" elements, "+TIMES+" times): "+time+"ms");
}
{
long time = -System.currentTimeMillis();
for(int i=TIMES; i-->0;)
copyPureJava(arr1, arr2, COUNT);
time += System.currentTimeMillis();
System.out.println("Result for pure java copiing ("+COUNT+" elements, "+TIMES+" times): "+time+"ms");
}
}
private static void copyReflect(Object src, Object dst, int count) {
for(int i=count; i-->0; ) Array.set(dst, i, Array.get(src, i));
}
private static void copySystem(Object src, Object dst, int count) {
System.arraycopy(src, 0, dst, 0, count);
}
private static void copyPureJava(String[] src, String[] dst, int count) {
for(int i=count; i-->0; ) dst[i] = src[i];
}
}
Ceb... I believe you ( not that I ever doubted you ), but
I hate to say your computer is **** slow. These were my
results. The ratios compared to yours are similar, but your
reflect copying does seem remarkably slower. These results were
with java 1.3.1_04 on a Toshiba laptop 1.7 p4 running xp pro.
Result for reflect copiing (3 elements, 1000000 times): 1051ms
Result for system copiing (3 elements, 1000000 times): 100ms
Result for pure java copiing (3 elements, 1000000 times): 81ms
> Ceb... I believe you ( not that I ever doubted you ),
> but
> I hate to say your computer is **** slow. These were
> my
> results. The ratios compared to yours are similar,
> but your
> reflect copying does seem remarkably slower.
AVa-ay! I'm happy about you, about your laptop and all your family. If you code for personal use only, you can use computer as fast as you want. But if you work for others, you need to test all your programs on slow computers also. Or you'll be such stupid like Billy which wrote that f...*** xp and believe that all people in the world have PIV with 2GHz processor, 4Gb of RAM and 200Gb hard...
ceb: aye... i hear you brother and couldn't agree more. i am lucky enough that i don't need to test for anyone but myself, and the run times on my computer are all that matters to be honest. and with this post, all run times prove the point you were making whether they are run on a 333 mhz win chip, or a 1.9 athlon xp, or a 2.2 ghz p4... i.e., coding is better than reflection. best of luck... and i hope for your sake you do have access to a few "quicker" machines occasionally, sort of like having a fast car. ; 0 )