Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in

Sequence

Searching for a value

Core exercise

Write indiceDe(int[] tableau, int valeur) that returns the index of the first occurrence of valeur, or -1 if it is absent.

A workshop goes here: you write and run code in it. It requires JavaScript.

Starting code

Exercices.java

public class Exercices {

    public static void main(String[] args) {
        int[] t = { 4, 8, 15, 16, 23, 42 };
        System.out.println("15 est a l'indice " + indiceDe(t, 15));
        System.out.println("99 est a l'indice " + indiceDe(t, 99));
    }

    public static int indiceDe(int[] tableau, int valeur) {
        // TODO
        return -1;
    }
}

The core exercise is validated in the workshop, which requires JavaScript.

Hint 1

Here the index matters: a classic for loop with i. Returning -1 for “absent” is a convention you will find again in indexOf.