给定一个由N个元素组成的数组和一个元素K,请在Java找到数组元素的索引。
例子:
Input: a[] = { 5, 4, 6, 1, 3, 2, 7, 8, 9 }, K = 5
Output: 0
Input: a[] = { 5, 4, 6, 1, 3, 2, 7, 8, 9 }, K = 7
Output: 6
可以使用以下方法搜索N个整数数组中的元素。
- 线性搜索:在数组中进行线性搜索,可以以O(N)复杂度找到元素。
以下是线性搜索方法的实现:
Java
// Java program to find index of
// an element in N elements
import java.util.*;
public class index {
// Linear-search function to find the index of an element
public static int findIndex(int arr[], int t)
{
// if array is Null
if (arr == null) {
return -1;
}
// find length of array
int len = arr.length;
int i = 0;
// traverse in the array
while (i < len) {
// if the i-th element is t
// then return the index
if (arr[i] == t) {
return i;
}
else {
i = i + 1;
}
}
return -1;
}
// Driver Code
public static void main(String[] args)
{
int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
// find the index of 5
System.out.println("Index position of 5 is: "
+ findIndex(my_array, 5));
// find the index of 7
System.out.println("Index position of 7 is: "
+ findIndex(my_array, 7));
}
}
Java
// Java program to find index of
// an element in N elements
import java.util.Arrays;
public class index {
// Function to find the index of an element
public static int findIndex(int arr[], int t)
{
int index = Arrays.binarySearch(arr, t);
return (index < 0) ? -1 : index;
}
// Driver Code
public static void main(String[] args)
{
int[] my_array = { 1, 2, 3, 4, 5, 6, 7 };
// find the index of 5
System.out.println("Index position of 5 is: "
+ findIndex(my_array, 5));
// find the index of 7
System.out.println("Index position of 7 is: "
+ findIndex(my_array, 7));
}
}
Java
// Java program to find index of
// an element in N elements
import java.util.List;
import com.google.common.primitives.Ints;
public class index {
// Function to find the index of an element using
public static int findIndex(int arr[], int t)
{
return Ints.indexOf(arr, t);
}
// Driver Code
public static void main(String[] args)
{
int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
System.out.println("Index position of 5 is: "
+ findIndex(my_array, 5));
System.out.println("Index position of 7 is: "
+ findIndex(my_array, 7));
}
}
Java
// Java program to find index of
// an element in N elements
import java.util.stream.IntStream;
public class index {
// Function to find the index of an element
public static int findIndex(int arr[], int t)
{
int len = arr.length;
return IntStream.range(0, len)
.filter(i -> t == arr[i])
.findFirst() // first occurrence
.orElse(-1); // No element found
}
public static void main(String[] args)
{
int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
System.out.println("Index position of 5 is: "
+ findIndex(my_array, 5));
System.out.println("Index position of 7 is: "
+ findIndex(my_array, 7));
}
}
Java
// Java program for the above approach
import java.util.ArrayList;
public class GFG {
public static int findIndex(int arr[], int t)
{
// Creating ArrayList
ArrayList clist = new ArrayList<>();
// adding elements of array
// to ArrayList
for (int i : arr)
clist.add(i);
// returning index of the element
return clist.indexOf(t);
}
// Driver program
public static void main(String[] args)
{
int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
System.out.println("Index position of 5 is: "
+ findIndex(my_array, 5));
System.out.println("Index position of 7 is: "
+ findIndex(my_array, 7));
}
}
Java
// Java program for the above approach
public class GFG {
public static int index(int arr[], int t, int start)
{
// base case when
// start equals the length of the
// array we return -1
if(start==arr.length)
return -1;
// if element at index start equals t
// we return start
if(arr[start]==t)
return start;
// we find for the rest
// position in the array
return index(arr,t,start+1);
}
public static int findIndex(int arr[], int t)
{
return index(arr,t,0);
}
// Driver Code
public static void main(String[] args)
{
int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
System.out.println("Index position of 5 is: "
+ findIndex(my_array, 5));
System.out.println("Index position of 7 is: "
+ findIndex(my_array, 7));
}
}
Index position of 5 is: 0
Index position of 7 is: 6
2.二进制搜索:二进制搜索也可用于查找数组中数组元素的索引。但是只有在对数组排序后才能使用二进制搜索。 Java为我们提供了一个内置函数,可以在Java的Arrays库中找到该函数,如果存在该元素,它将返回索引,否则返回-1。复杂度将为O(log n)。
下面是二进制搜索的实现。
Java
// Java program to find index of
// an element in N elements
import java.util.Arrays;
public class index {
// Function to find the index of an element
public static int findIndex(int arr[], int t)
{
int index = Arrays.binarySearch(arr, t);
return (index < 0) ? -1 : index;
}
// Driver Code
public static void main(String[] args)
{
int[] my_array = { 1, 2, 3, 4, 5, 6, 7 };
// find the index of 5
System.out.println("Index position of 5 is: "
+ findIndex(my_array, 5));
// find the index of 7
System.out.println("Index position of 7 is: "
+ findIndex(my_array, 7));
}
}
Index position of 5 is: 4
Index position of 7 is: 6
3 。番石榴 Guava是Google开发的基于Java的开源库。它提供了用于收集,缓存,原语支持,并发,通用批注,字符串处理,I / O和验证的实用程序方法。 Guava提供了几个与原始类型相关的实用程序类,例如Ints表示int,Longs表示long,Doubles表示double等。每个实用程序类都有一个indexOf()方法,该方法返回元素在数组中首次出现的索引。
以下是番石榴的实现。
Java
// Java program to find index of
// an element in N elements
import java.util.List;
import com.google.common.primitives.Ints;
public class index {
// Function to find the index of an element using
public static int findIndex(int arr[], int t)
{
return Ints.indexOf(arr, t);
}
// Driver Code
public static void main(String[] args)
{
int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
System.out.println("Index position of 5 is: "
+ findIndex(my_array, 5));
System.out.println("Index position of 7 is: "
+ findIndex(my_array, 7));
}
}
Index position of 5 is: 0
Index position of 7 is: 6
4. Stream API: Stream是Java 8中引入的新的抽象层。使用stream,可以以类似于SQL语句的声明性方式处理数据。该流表示来自源的一系列对象,该对象支持聚合操作。为了找到元素的索引,Stream包提供了实用程序IntStream。使用数组的长度,我们可以获得一个从0到n-1的数组索引的IntStream,其中n是数组的长度。
下面是Stream API方法的实现。
Java
// Java program to find index of
// an element in N elements
import java.util.stream.IntStream;
public class index {
// Function to find the index of an element
public static int findIndex(int arr[], int t)
{
int len = arr.length;
return IntStream.range(0, len)
.filter(i -> t == arr[i])
.findFirst() // first occurrence
.orElse(-1); // No element found
}
public static void main(String[] args)
{
int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
System.out.println("Index position of 5 is: "
+ findIndex(my_array, 5));
System.out.println("Index position of 7 is: "
+ findIndex(my_array, 7));
}
}
Index position of 5 is: 0
Index position of 7 is: 6
5.使用ArrayList
在这种方法中,我们将数组转换为ArrayList,然后使用ArrayList的indexOf方法获取元素的索引。
Java
// Java program for the above approach
import java.util.ArrayList;
public class GFG {
public static int findIndex(int arr[], int t)
{
// Creating ArrayList
ArrayList clist = new ArrayList<>();
// adding elements of array
// to ArrayList
for (int i : arr)
clist.add(i);
// returning index of the element
return clist.indexOf(t);
}
// Driver program
public static void main(String[] args)
{
int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
System.out.println("Index position of 5 is: "
+ findIndex(my_array, 5));
System.out.println("Index position of 7 is: "
+ findIndex(my_array, 7));
}
}
输出:
Index position of 5 is: 0
Index position of 7 is: 6
6.使用递归
我们将使用递归来找到给定元素的第一个索引。
Java
// Java program for the above approach
public class GFG {
public static int index(int arr[], int t, int start)
{
// base case when
// start equals the length of the
// array we return -1
if(start==arr.length)
return -1;
// if element at index start equals t
// we return start
if(arr[start]==t)
return start;
// we find for the rest
// position in the array
return index(arr,t,start+1);
}
public static int findIndex(int arr[], int t)
{
return index(arr,t,0);
}
// Driver Code
public static void main(String[] args)
{
int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
System.out.println("Index position of 5 is: "
+ findIndex(my_array, 5));
System.out.println("Index position of 7 is: "
+ findIndex(my_array, 7));
}
}
输出:
Index position of 5 is: 0
Index position of 7 is: 6