Question

I am new to java. I am creating a school project in which I am using arrays.
My question is:
what is better - if i sort an array it also takes time in sorting.So will it be good to leave it unsorted for my small school project and while retrieving i will put some logic to retrive the desired array value.

Was it helpful?

Solution

It's better to keep it sorted if you think you will need sorted values at some later stage.
It would be better if you would paste your code here.

OTHER TIPS

Either you sorted or not it is necessary to apply a logic to retrieve values from an array. If your array not a very large one you may not want to bother about performance since sorting doesn't take much time for smaller arrays.

It is better to use List over arrays since it has an implementation called ArrayList which grows dynamically.

If it is possible, may be is better use a vector

Have you think what would happen when you insert a new item in your array? Or if you change the way of sorting? If you keep the array sorted, keep in mind this things.

For these reasons, I think is better to sort the array each time you need to return a value (the best is using Vectors, as I said at the beginning).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top