Homework Questions? Ask a Tutor for Answers ASAP
I just want to be able to do this in Java like I can in C
Let me say that I have defined a class/structure type that I call a tylertown.
A single tylertown occupies 2 kilobytes of memory, and is made up of parts (fields) that are no more than 16 bytes each. I have an array of 600 tylertowns, and have defined a function that determines, for any two unequal tylertowns, which one is greater, and I want to sort these 600 tylertowns in ascending order. In C/C++, I would declare an array of 600 pointers to tylertowns, and assign every pointer in that array to a different one of the 600 tylertowns, then sort the 600 tylertowns by an algorithm that requires a series of swaps between tylertowns, but I would instead swap the pointers to those tylertowns involved every time a swap was made, until finally the pointers were in the correct order, and then copy the 600 tylertowns to another declared tylertown array in the correct order. If I didn't use pointers, it would take much longer, because at every swap I would have to transfer 2 kilobytes of information between variables instead of a single address between variables. So how can I do this sort with similar speed using Java?
I don't have a deadline
In java, there are no pointers. These are not needed as in java, we have objects which can point to(refer) similar type of objects. Whenever we sort an array of objects in Java, it swaps objects using references (until we use deep copy). This is like using pointers in c/c++.
If we use primitive types like int, float etc, swapping will take time. Java provides wrapper classes. We can wrap primitive type in its wrapper class and perform swapping.
One thing is obvious, Java will never be as faster as C++. Hope it clears
Are you saying that if I swap the entire structure every time, in my example by assigning a declared tylertown the value of another tylertown, that behind the scenes Java assigns references, speeding the process up? If not, how can I temporarily copy (opposite of "deep copy") during the sort and then "deep copy" at the end?