Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Mark on 24/02/2017.
- CharArraySearchDemo
- */
- public class CharArraySearch {
- public static void main(String[] args){
- char[] chars1 = {'h', 'e', 'l','l', 'o'};
- int trump = find01(chars1, 1, 3, 'l');
- int trump1 = find02(chars1, 1, 3, 'l');
- int trump2 = find03(chars1, 1, 3, 'l');
- int trump3 = find04(chars1, 1, 3, 'l');
- System.out.println("The match is found at index " + trump);
- System.out.println("The match is found at index " + trump1);
- System.out.println("The match is found at index " + trump2);
- System.out.println("The match is found at index " + trump3);
- }
- public static int find01(char[] arr, int start, int end, char target){
- for (;start <= end ; start++){
- if(arr[start] == target) {
- return start;
- }
- }
- return -1;
- }
- public static int find02(char[] arr, int start, int end, char target){
- int index = -1;
- for(;start <= end; start++){
- if(arr[start]== target){
- index = start;
- }
- }
- return index;
- }
- public static int find03(char[] arr, int start, int end, char target){
- for(;end >= start; end--){
- if(arr[end] == target){
- return end;
- }
- }
- return -1;
- }
- public static int find04(char[] arr, int start, int end, char target){
- int index = -1;
- for(;end >= start; end--){
- if(arr[end] == target){
- index = end;
- }
- }
- return index;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement