Skip to main content
added 92 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

Local scope, as it is now. UnlessI'm not sure if you can gain anything at all by not recreating the ArrayList everytime. The performance difference should be negligible, and unless micro benchmarkingoptimization is absolutely critical for your use case, you don't need to worry about the fact that the array gets thrown away at every runit.

Local scope, as it is now. Unless micro benchmarking is absolutely critical for your use case, you don't need to worry about the fact that the array gets thrown away at every run.

Local scope, as it is now. I'm not sure if you can gain anything at all by not recreating the ArrayList everytime. The performance difference should be negligible, and unless micro optimization is absolutely critical for your use case, you don't need to worry about it.

deleted 15 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

The randomArrayList does 2 things now: it generates a list of random values that is sorted. You could change this to do only one thing: generate a list of random values that is sorted. Let's rename it generateRandomList.

The randomArrayList does 2 things now: it generates a list of random values that is sorted. You could change this to do only one thing: generate a list of random values that is sorted. Let's rename it generateRandomList.

The randomArrayList does 2 things now: it generates a list of random values that is sorted. You could change this to do only one thing: generate a list of random values. Let's rename it generateRandomList.

added 656 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

Your other questions

The other thing is I'm having trouble knowing how to name the array that is to be filled with random values, I can't think of a better name than data.

I renamed the randomArrayList method to generateRandomList, and it felt quite natural to rename the data variable to randomList.

Last thing, should the variable data be in the local scope of the method or should it be a member variable of the class? I would never have more than one array I'm using at a time, for example I wouldn't have sorted and reverse sorted array I need to hold on to the values to after I use them once.

Local scope, as it is now. Unless micro benchmarking is absolutely critical for your use case, you don't need to worry about the fact that the array gets thrown away at every run.

Suggested implementation

I'm pasting this here mainly for the unit tests. Before refactoring your methods I wrote the tests to make sure I'm not breaking anything. Having these tests in advance are a real time saver, I recommend you start using them.

Suggested implementation

Your other questions

The other thing is I'm having trouble knowing how to name the array that is to be filled with random values, I can't think of a better name than data.

I renamed the randomArrayList method to generateRandomList, and it felt quite natural to rename the data variable to randomList.

Last thing, should the variable data be in the local scope of the method or should it be a member variable of the class? I would never have more than one array I'm using at a time, for example I wouldn't have sorted and reverse sorted array I need to hold on to the values to after I use them once.

Local scope, as it is now. Unless micro benchmarking is absolutely critical for your use case, you don't need to worry about the fact that the array gets thrown away at every run.

Suggested implementation

I'm pasting this here mainly for the unit tests. Before refactoring your methods I wrote the tests to make sure I'm not breaking anything. Having these tests in advance are a real time saver, I recommend you start using them.

Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396
Loading