Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. String[] stackSample = {"a", "b", "c", "a", "tacos"};
  7. String needleSample = "a";
  8. System.out.println(countOccurrences(stackSample, needleSample));
  9. System.out.println(firstOccurrence(stackSample, needleSample));
  10. int [] newArray = {1, 3, 5, 7};
  11. System.out.println(Arrays.toString(multiplyArray(newArray)));
  12. }
  13.  
  14. public static int countOccurrences(String[] stack, String needle) {
  15. int count = 0;
  16. for (int i=0; i < stack.length; i++) {
  17. if (stack[i].equals(needle)) {
  18. count +=1;
  19. }
  20. }
  21. return count;
  22. }
  23.  
  24. public static int firstOccurrence(String[] stack, String needle) {
  25. for (int i = 0; i < stack.length; i++) {
  26. if (stack[i].equals(needle)) {
  27. return i;
  28. }
  29.  
  30. }
  31. return -1;
  32. }
  33.  
  34. public static int[] multiplyArray(int[] values) {
  35. int[] result = new int[values.length];
  36. for (int i = 0; i < values.length; i++) {
  37. result[i] = values[i] * 2;
  38. }
  39. return result;
  40.  
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement