fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. private static int posOfTarget;
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. int [] arr = {1,2,3,4,5,6};
  14. int target = 5;
  15. if(contains(arr,target)){
  16. System.out.println(target+" found at pos " + getPosOfTarget()+1);
  17. }
  18. else
  19. System.out.println(target+" doesn't exist in arr ");
  20. }
  21.  
  22. public static boolean contains(int[] input, int target) {
  23. for(int i = 0; i < input.length; i++){
  24. if (target == input[i]){
  25. setPosofTarget(i);
  26. return true;
  27. }
  28. }
  29. return false;
  30. }
  31.  
  32. public static int indexOf(int[] input, int target) {
  33. if(contains(input, target) == true){
  34. //pos is already set in contains()
  35. return (getPosOfTarget());
  36. }
  37. else
  38. return -1;
  39. }
  40.  
  41. //getter setter of pos
  42. public static int getPosOfTarget(){
  43. return posOfTarget;
  44. };
  45. public static void setPosofTarget(int i){
  46. posOfTarget = i;
  47. };
  48. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
5 found at pos 41