Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement