Skip to content

Instantly share code, notes, and snippets.

@bastienapp
Last active September 27, 2019 14:01
Show Gist options
  • Select an option

  • Save bastienapp/8da3c3693044da79d152dbe4126f3113 to your computer and use it in GitHub Desktop.

Select an option

Save bastienapp/8da3c3693044da79d152dbe4126f3113 to your computer and use it in GitHub Desktop.
Exercices sur les tableaux
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[] stackSample = {"a", "b", "c", "a", "tacos"};
String needleSample = "a";
System.out.println(countOccurrences(stackSample, needleSample));
System.out.println(firstOccurrence(stackSample, needleSample));
int [] newArray = {1, 3, 5, 7};
System.out.println(Arrays.toString(multiplyArray(newArray)));
}
public static int countOccurrences(String[] stack, String needle) {
int count = 0;
for (int i=0; i < stack.length; i++) {
if (stack[i].equals(needle)) {
count +=1;
}
}
return count;
}
public static int firstOccurrence(String[] stack, String needle) {
for (int i = 0; i < stack.length; i++) {
if (stack[i].equals(needle)) {
return i;
}
}
return -1;
}
public static int[] multiplyArray(int[] values) {
int[] result = new int[values.length];
for (int i = 0; i < values.length; i++) {
result[i] = values[i] * 2;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment