Guest User

Untitled

a guest
Jul 22nd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. package oving1 ;
  2.  
  3.  
  4. public class ArrayMethods1 {
  5. static // find the smallest index of value in array
  6. int indexOf (int [] array , int value ) {
  7. int index=-1;
  8. for (int i=0;i<array.length; i++){
  9. if (array[i] == value){
  10. index=i;
  11. }
  12. }
  13. return index;
  14. }
  15. static // find the largest index of value in array
  16. int lastIndexOf (String [] array , String value ) {
  17. int index=-1;
  18. for (int i=-1; i<array.length; i--){
  19. if (array[i].equals(value)){
  20. index=i;
  21. }
  22. }
  23. return index;
  24. }
  25. static // find the index of the smallest value in array, starting at start
  26. int indexOfSmallest (int [] array , int start ) {
  27. int m = Integer.MAX_VALUE;
  28. int index = -1;
  29.  
  30. for(int i = start, l = array.length; i < l; i++){
  31.  
  32. if(array[i] < m){
  33.  
  34. m = array[i];
  35. index = i;
  36. }
  37. }
  38.  
  39. return index;
  40. }
  41.  
  42. static int [] intArray = { 1 , 2 , 3 , 4 , 3 , 2 , 1 } ;
  43. static String [] stringArray = { "1" , "2" , "3" , "4" , "3" , "2" , "1" } ;
  44. static {
  45. System .out .println ( "indexOf(intArray, 3) is " + indexOf ( intArray , 3 ) ) ;
  46. System .out .println ( "lastIndexOf(stringArray, \"3\") is " + lastIndexOf ( stringArray , "3" ) ) ;
  47. System .out .println ( "indexOfSmallest(intArray, 1) is " + indexOfSmallest ( intArray , 1 ) ) ;
  48. }
  49. public static void main (java.lang.String args [] ) {
  50. }
  51. }
Add Comment
Please, Sign In to add comment