如何根据给定的字符串值在 java 数组中找到字符串的索引
How to find the Index of string in java array from the given string value
creditTIDstatusArray=[93312263-1-09722612223, 99802001-1-09102842369, 99802002-1-09102842369];
creditTIDstatusList.addAll(Arrays.asList(creditTIDstatusArry));
searchValue="99802002-1".
int retval=creditTIDstatusList.indexOf("99802002-1");
System.out.println("The element at index is:" retval);
Output: 2
请告诉我如何找到上面给定的 (searchValue) 元素的索引。
因为您从未发布过可重现的代码。
假设
creditTIDstatusArray
是一个 String
类型的数组。
- 您的搜索查询始终位于数组中每个
String
值的前面。
- 多个索引可能以相同的搜索值开头。
String[] creditTIDstatusArray=new String[]{"93312263-1-09722612223", "99802001-1-09102842369", "99802002-1-09102842369"};
String searchValue="99802002-1";
for (int i = 0; i < creditTIDstatusArray.length; i++) {
if(creditTIDstatusArray[i].startsWith(searchValue)){
System.out.println("Index :" + I); // this will print all the indexes that starts with searchvalue
}
}
我不确定您为什么将数组添加到列表中然后搜索 index of
因为它永远不会工作,因为您只搜索 String
的一部分而不是整个 value
或 object
.
creditTIDstatusArray=[93312263-1-09722612223, 99802001-1-09102842369, 99802002-1-09102842369];
creditTIDstatusList.addAll(Arrays.asList(creditTIDstatusArry));
searchValue="99802002-1".
int retval=creditTIDstatusList.indexOf("99802002-1");
System.out.println("The element at index is:" retval);
Output: 2
请告诉我如何找到上面给定的 (searchValue) 元素的索引。
因为您从未发布过可重现的代码。 假设
creditTIDstatusArray
是一个String
类型的数组。- 您的搜索查询始终位于数组中每个
String
值的前面。 - 多个索引可能以相同的搜索值开头。
String[] creditTIDstatusArray=new String[]{"93312263-1-09722612223", "99802001-1-09102842369", "99802002-1-09102842369"};
String searchValue="99802002-1";
for (int i = 0; i < creditTIDstatusArray.length; i++) {
if(creditTIDstatusArray[i].startsWith(searchValue)){
System.out.println("Index :" + I); // this will print all the indexes that starts with searchvalue
}
}
我不确定您为什么将数组添加到列表中然后搜索 index of
因为它永远不会工作,因为您只搜索 String
的一部分而不是整个 value
或 object
.