在Java中,你可以使用以下几种方法来查找数组中元素的原始位置(索引):
线性搜索:
```java
public static int linearSearch(int[] arr, int value) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return i;
}
}
return -1; // 如果值不存在于数组中,返回-1
}
二分搜索:
```java
public static int binarySearch(int[] arr, int value) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == value) {
return mid;
} else if (arr[mid] < value) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1; // 如果值不存在于数组中,返回-1
}
使用Java库方法:
```java
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int value = 3;
int index = Arrays.binarySearch(arr, value);
System.out.println("The index of " + value + " is: " + index);
}
}
以上方法都可以用来查找数组中元素的索引位置。请根据你的具体需求选择合适的方法