Rewriting this working Java array to make it easier, better or more efficient, without ArrayList

Go To StackoverFlow.com

2

G'day

I have a Java assignment for a introduction course to object oriented programming. I have finished the assignment, and the program works as expected (took me a while to achieve this!) and I get the desired output. Just with one of my methods I have used 2 FOR loops with an IF statement, and I wondering if there is another way that is easier, better or more efficient? But I am not allowed to use an ArrayList. I understand that the ArrayList is easier, better and more efficient, but without being able to use the ArrayList, is it possible to make my code better and still achieve the same result?

Basically I am requesting the user to type an ID number of a property object, then matching that ID to the array index position, copying that object from one array (propertiesForSale) to another array (propertiesSold), adding an increment of 1 to my counter (numPropertiesSold) before deleting and compacting (my teacher said reshuffling) the the first (propertiesForSale) array and decrementing my other counter (numPropertiesForSale) by 1.

My code is as follows..

public void sellProperty(int inID)
    {
         for (int index = 0; index < numPropertiesForSale; index++) {

            if (propertiesForSale[index].getId() == inID) {

                propertiesSold[numPropertiesSold]= propertiesForSale[index];
                numPropertiesSold++;

                for (int i = index; i < numPropertiesForSale; i++) {

                propertiesForSale[i] = propertiesForSale[i + 1];
                }

                numPropertiesForSale--;
            }
        }
    }

If there is an easier, better or more efficient way WITHOUT using an ArrayList, I am just curious to see if there is and how it would be done.

Thanks for your time, patience and input (if any).

2012-04-05 01:07
by bigdal
if doesn't loop - Niklas B. 2012-04-05 01:15
Ah yes indeed, thanks for pointing that out - bigdal 2012-04-05 01:55
Are you also limited in using Maps? This would be a lot nicer with an K->V model than a linear array structure as well as more efficien - maasg 2012-04-05 08:27


3

If you're not allowed to use ArrayList that looks to me to be pretty much the best you can do.

Though rather than using a loop to shift the propertiesForSale array down one you might want to look into using System.arraycopy() instead of your own loop, since arraycopy() is a native method.

2012-04-05 01:14
by QuantumMechanic
Thank you, I shall research the System.arrayCopy on the API and have a play around with it - bigdal 2012-04-05 01:56


1

You could use your own custom linked list made out of nodes. That way you don't need to shift the entries of the array when you sell a property; you just remove the node from the list and update the pointers.

2012-04-05 01:16
by Adrian
A custom linked list made out of nodes? I have no idea what that is but I shall google it and see if I can work it out and play around with it. Thanks - bigdal 2012-04-05 01:57
@bigdal google linked lis - Adrian 2012-04-05 02:45


1

You are searching for inID in propertiesForSale. When you find a match you transfer that object to the next index in propertiesSold and then you compact the array by transferring all records above it to one index lower. The last element is just left there, but it won't be accessed in next lookup because you decrement the count.

Another option is to not compact the array at all, but instead just set it to null. You will of course have to adjust the max bound in the outer for loop and add a non-null check but the inner for loop is gone.

Is it better? Maybe. What you have got is pretty good.

2012-04-05 01:23
by Miserable Variable
Thanks for the reply. I am trying to understand and use the correct terminology. So technically I am compacting the array? Not moving the object between the two arrays? Do you mean that I am compacting the array by deleting that array index and thus compacting the rest of the array indexes? I realise now I should have said I am copying the object to the other array, as I am not actually moving it - bigdal 2012-04-05 02:06
Technically you are not compacting, because the array size is not changing. Yes, you are copying the reference from one array to another. But after that you are moving the elements down. I was suggesting you can avoid that, at the cost of extra null checks - Miserable Variable 2012-04-05 18:16


1

Test your code by filling completely filling the propertiesForSale array before selling a property. That is, what will happen if numPropertiesForSale == propertiesForSale.length when you try to remove an element?

2012-04-05 06:13
by erickson
Before I finished this code I believe I had an out of bounds exception with the scenario you suggested. It took me a while to realise that was what was happening, as I couldn't work out why it wouldn't work! I have a variable MAX_PROPERTIES which is set to 10 and the total properties is only 5 (these are all set by the teacher). I think that was the point of not using ArrayList, so we understand what was going on behind the scenes so to speak. Please correct me if I am wrong - bigdal 2012-04-09 06:18
Ads