For readability, you could extract these two concerns to 2 methods that take as input the array and J. They would spit out the new J after they've made the modification to the array.