2017-06-02 1 views
-3

J'essaie d'imprimer la valeur de l'index de chaque valeur dans ce tableau.indexOf() avec valeur répétée dans le tableau Java

Integer[] i = { 0, 2, 3, 4, 4 }; 
    for (int each : i) { 
     System.out.print(Arrays.asList(i).indexOf(each) + " "); 
    } 

Maintenant, je veux dire qu'il 0, 1, 2, 3, 4, non 0, 1, 2, 3, 3.

Que dois-je changer pour obtenir chaque index spécifique, et non la première "correspondance" de chaque valeur?

+0

Je suis confus ce que vous essayez d'obtenir, si vous itérez à travers le tableau un à la fois, logiquement, l'indice de chaque itération sera le même que votre valeur «i» ... – Reed

+1

@ Aominè ah, merci pour la clarification. – Reed

Répondre

0

Si vous avez des valeurs dupliquées dans la liste, vous retournerez la première valeur correspondante dans votre implémentation. Pour résoudre ce problème, vous pouvez créer un tableau de copie et, lorsque la valeur est trouvée dans la copie, le remplacer par "null".

La valeur dupliquée la prochaine fois ne trouvera pas la première copie et retournera l'index suivant.

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; 
     } 

sortie: 0 1 2 3 4

Bien sûr, vous pouvez maintenant modifier l'ordre du tableau initial et il retournera encore des index corrects.

1

Je ne sais pas ce que vous essayez d'accomplir avec votre code, mais si vous voulez imprimer tous les indices du tableau et non les valeurs que vous pouvez le faire

Integer[] i = {0,2,3,4,4}; 
for (int index = 0; index < i.length; index++) 
{ 
    System.out.print(index + " "); 
} 
0

Si vous voulez obtenir le Index de un élément spécifique dans un tableau, vous pouvez faire quelque chose comme ceci:

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; 
} 

sortie

1 
2 
-1 

De plus, si vous voulez retourner TOUS Indices trouvés pour un répété élément dans un tableau, vous pouvez faire quelque chose comme ceci:

/** 
* 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; 
} 

Test

// Create example Array with duplicates 
String[] arrayHasDuplicates = {"A", "B", "C", "D", "B", "Y", "B"}; 
System.out.println(getIndices(arrayHasDuplicates, "B")); 

Output: 
[1, 4, 6]