Java数组中具有重复值的indexOf()
我试图打印此数组中每个值的索引值Java数组中具有重复值的indexOf(),java,arrays,indexof,Java,Arrays,Indexof,我试图打印此数组中每个值的索引值 Integer[] i = { 0, 2, 3, 4, 4 }; for (int each : i) { System.out.print(Arrays.asList(i).indexOf(each) + " "); } 现在,我想让它说0,1,2,3,4,而不是0,1,2,3,3 要获得每个特定索引,而不是每个值的“第一个匹配”,我需要更改什么?如果列表中有重复的值,则将在实现中返回第一个匹配值。要解决此问题,您可以创建
Integer[] i = { 0, 2, 3, 4, 4 };
for (int each : i) {
System.out.print(Arrays.asList(i).indexOf(each) + " ");
}
现在,我想让它说0,1,2,3,4,而不是0,1,2,3,3
要获得每个特定索引,而不是每个值的“第一个匹配”,我需要更改什么?如果列表中有重复的值,则将在实现中返回第一个匹配值。要解决此问题,您可以创建一个副本数组,当在副本中找到值时,将其替换为null 下次重复的值将找不到第一个重复项,并将返回下一个索引
Integer[] i = {0,2,3,4,4};
Integer[] copy = new Integer[i.length];
System.arraycopy( i, 0, copy, 0, i.length );
for (int each : i) {
int index = Arrays.asList(copy).indexOf(each);
System.out.print(index + " ");
copy[index] = null;
}
输出:01 2 3 4
当然,现在您可以对初始数组重新排序,它仍然会返回正确的索引 不确定您试图用代码实现什么,但如果您想打印数组的所有索引,而不是值,您可以这样做
Integer[] i = {0,2,3,4,4};
for (int index = 0; index < i.length; index++)
{
System.out.print(index + " ");
}
如果要获取数组中特定项的索引,可以执行以下操作:
public static void main(String[] args){
// Create example Arrays
Integer[] arrayOfIntegers = {1,2,3,4,5};
String[] arrayOfStrings = {"A", "B", "C", "D",};
/******Test*******/
// what is the index of Integer 2 in the arrayOfIntegers
System.out.println(getIndex(arrayOfIntegers, 2));
// what is the index of String "C" in the arrayOfStrings
System.out.println(getIndex(arrayOfStrings, "C"));
// what is the index of String "X" in the arrayOfIStrings
// which doesn't exist (expected value is -1)
System.out.println(getIndex(arrayOfStrings, "X"));
}
/**
* This method to return the index of a given Object in
* the array. It returns - 1 if doesn't exist
* @param array
* @param obj
*/
public static int getIndex(Object[] array, Object obj){
for(int i=0; i<array.length; i++){
if(obj.equals(array[i])){
return i;
}
}
return -1;
}
/**
* This method returns all indices of a given Object
* in an ArrayList (which will be empty if did not found any)
* @param array
* @param obj
*/
public static ArrayList<Integer> getIndices(Object[] array, Object obj){
ArrayList<Integer> indices = new ArrayList<Integer>();
for(int i=0; i<array.length; i++){
if(obj.equals(array[i])){
indices.add(i);
}
}
return indices;
}
此外,如果要返回数组中重复项的所有索引,可以执行以下操作:
public static void main(String[] args){
// Create example Arrays
Integer[] arrayOfIntegers = {1,2,3,4,5};
String[] arrayOfStrings = {"A", "B", "C", "D",};
/******Test*******/
// what is the index of Integer 2 in the arrayOfIntegers
System.out.println(getIndex(arrayOfIntegers, 2));
// what is the index of String "C" in the arrayOfStrings
System.out.println(getIndex(arrayOfStrings, "C"));
// what is the index of String "X" in the arrayOfIStrings
// which doesn't exist (expected value is -1)
System.out.println(getIndex(arrayOfStrings, "X"));
}
/**
* This method to return the index of a given Object in
* the array. It returns - 1 if doesn't exist
* @param array
* @param obj
*/
public static int getIndex(Object[] array, Object obj){
for(int i=0; i<array.length; i++){
if(obj.equals(array[i])){
return i;
}
}
return -1;
}
/**
* This method returns all indices of a given Object
* in an ArrayList (which will be empty if did not found any)
* @param array
* @param obj
*/
public static ArrayList<Integer> getIndices(Object[] array, Object obj){
ArrayList<Integer> indices = new ArrayList<Integer>();
for(int i=0; i<array.length; i++){
if(obj.equals(array[i])){
indices.add(i);
}
}
return indices;
}
我不知道你想得到什么,如果你一次迭代一个数组,从逻辑上讲,每次迭代的索引将与你的I值相同…@Aominèah,谢谢你的澄清。