Home > Blockchain >  Get Index number of an element for all occurences
Get Index number of an element for all occurences

Time:10-18

I would like to get the index number of an element, the following code gets the first occurrence only.

Array content for hair:

short,
long,
long

What index number I get: 1

What index number is needed: 1, 2


            for(int i = 0; i <= hair.length; i=i 1) {
                if(hair[i].equalsIgnoreCase("long")) {
                    index = i;
                    break;
                }
           }

CodePudding user response:

You could do something like this

    public static void main(String[] args) {
        String[] hair = {"short", "long", "long"}; 
        int[] indexes = new int[hair.length];
        for(int i = 0; i < hair.length; i  ) {
            if(hair[i].equalsIgnoreCase("long")) {
                indexes[i] = i;
            }else{
                indexes[i] = -1;
            }
        }
           
       for(int i = 0; i < indexes.length; i  ) {
            if(indexes[i]!=-1){
                System.out.println(indexes[i]);
            }
       }
    }

Demo

CodePudding user response:

You may use a container such as a List to store each valid index, also careful the stop condition should be i < hair.length

String[] hair = {"short", "long", "long"};
List<Integer> result = new ArrayList<>();
for (int i = 0; i < hair.length; i  ) {
    if (hair[i].equalsIgnoreCase("long")) {
        result.add(i);
    }
}
System.out.println(result); // [1, 2]

Use a int[] to store is a bit different, as you'll have less valid index as the size of the array, ending with empty box at the end (0 value), you'll need a Arrays.copyof to truncate it

String[] hair = {"short", "long", "long", "short", "long", "long", "short", "long", "long"};
int[] indices = new int[hair.length];
int nb_valid = 0;
for (int i = 0; i < hair.length; i  ) {
    if (hair[i].equalsIgnoreCase("long")) {
        indices[nb_valid  ] = i;
    }
}
System.out.println(Arrays.toString(indices)); // [1, 2, 4, 5, 7, 8, 0, 0, 0]

indices = Arrays.copyOf(indices, nb_valid); // truncate the array to keep real indices only
System.out.println(Arrays.toString(indices)); // [1, 2, 4, 5, 7, 8]

CodePudding user response:

The "break;" inside the loop is breaking the whole "for" loop.

As said, don't override the "index" param (instead, use some datastructure to hold more than 1 object), and remove the "break", so you can iterate all elements of the "hair" array.

CodePudding user response:

You may use an int array to hold the index values.

Here's the input

{ "short", "long", "long", "short", "long" }

Here's the output

1 2 4

Here's the complete runnable code.

public class FindMatches {

    public static void main(String[] args) {
        String[] hair = { "short", "long", "long", "short", "long" };
        int[] indexValues = new int[hair.length];
        int index = 0;

        for (int i = 0; i < hair.length; i  ) {
            if (hair[i].equalsIgnoreCase("long")) {
                indexValues[index  ] = i;
            }
        }

        for (int i = 0; i < index; i  ) {
            System.out.print(indexValues[i]);
            if (i < (index - 1)) {
                System.out.print(" ");
            }
        }

        System.out.println();
    }

}

CodePudding user response:

You can also use streams for finding matching indices

String[] hair = {"short", "long", "long"};
int[] indices = IntStream.range(0, hair.length)
            .filter(i -> "long".equalsIgnoreCase(hair[i]))
            .toArray();
  • Related