Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package oving1 ;
- public class ArrayMethods1 {
- static // find the smallest index of value in array
- int indexOf (int [] array , int value ) {
- int index=-1;
- for (int i=0;i<array.length; i++){
- if (array[i] == value){
- index=i;
- }
- }
- return index;
- }
- static // find the largest index of value in array
- int lastIndexOf (String [] array , String value ) {
- int index=-1;
- for (int i=-1; i<array.length; i--){
- if (array[i].equals(value)){
- index=i;
- }
- }
- return index;
- }
- static // find the index of the smallest value in array, starting at start
- int indexOfSmallest (int [] array , int start ) {
- int m = Integer.MAX_VALUE;
- int index = -1;
- for(int i = start, l = array.length; i < l; i++){
- if(array[i] < m){
- m = array[i];
- index = i;
- }
- }
- return index;
- }
- static int [] intArray = { 1 , 2 , 3 , 4 , 3 , 2 , 1 } ;
- static String [] stringArray = { "1" , "2" , "3" , "4" , "3" , "2" , "1" } ;
- static {
- System .out .println ( "indexOf(intArray, 3) is " + indexOf ( intArray , 3 ) ) ;
- System .out .println ( "lastIndexOf(stringArray, \"3\") is " + lastIndexOf ( stringArray , "3" ) ) ;
- System .out .println ( "indexOfSmallest(intArray, 1) is " + indexOfSmallest ( intArray , 1 ) ) ;
- }
- public static void main (java.lang.String args [] ) {
- }
- }
Add Comment
Please, Sign In to add comment