This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello, i have a double array and i want to replace an item in the array. My method below takes a double from the user and checks if it exists, and when the element does exist, how do i find it's index and replace it with the value provided from the user?
You have a couple of options, you could sort the array and call java.util.Arrays.binarySearch(score), if the return value is > -1 that is the index of your element. If the ordering of the array needs to be maintained, you can write a method that does a linear search of the array, and if it finds a particular element, have the method reurn the index of element, else have it return some value that cannot be an index, like -1.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Joined: Oct 21, 2006
That gave me a little insight but i'm still having trouble finding the exact index of what the user typed in. If in the array i have the values:
1 2 3
and the user wants to replace the value 2 (which is at index 1) with let's say the value 5, how can that be done?