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

G'day

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

Basically I ask the user to enter the id of the property object and then map that id to the index position of the array by 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 rearranging) the first (propertiesForSale) array and decreasing my other counter (numPropertiesForSale) by 1.

My code looks like this.

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'm just curious to know if there is and how to do this.

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

+3


source to share


4 answers


If you are not allowed to use ArrayList

that looks to me the best thing you can do.



Although, instead of using a loop to move the array propertiesForSale

down, you might want to use it System.arraycopy()

instead of your own loop, since it arraycopy()

is a native method.

+3


source


You can use your own linked list created from nodes. This way, you do not need to move the array elements when selling property; you just remove the node from the list and update the pointers.



+1


source


You are looking inID

in propertiesForSale

. When you find a match, you move that object to the next index in propertiesSold

, and then you compact the array, moving all records above it one index below. The last item is just left there, but it won't be available in the next search because you will decrease the score.

Another option is to not combine the array at all, but instead just set it to null. You will of course need to adjust the maximum binding in the outer loop and add a nonzero check, but the inner loop hasn't disappeared.

This is better? May be. What you have is pretty good.

+1


source


Test your code by completely filling the array propertiesForSale

before selling a property. That is, what happens if numPropertiesForSale == propertiesForSale.length

you try to delete an item?

+1


source







All Articles