Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class StringFilter {
  4.     public static void main(String[] args) {
  5.        
  6.         String[] arr = {"Alice", "Sue", "Janet", "Bea"};
  7.         System.out.println(Arrays.toString(arr));
  8.        
  9.        
  10.         String[] a1 = SFilter.filter(arr, new LenFilter(3));
  11.         System.out.println(Arrays.toString(a1));
  12.        
  13.        
  14.         String[] a2 = SFilter.filter(arr, new SFilter() {
  15.             public boolean test(String s) {
  16.                 return (s.charAt(0) < 'D' && s.charAt(0) >='A');
  17.             }
  18.         });
  19.         System.out.println(Arrays.toString(a2));
  20.        
  21.        
  22.         String[] a3 = SFilter.filter(arr, s -> s.charAt(0) > 'H' && s.charAt(0) <= 'Z');
  23.         System.out.println(Arrays.toString(a3));
  24.     }
  25. }
  26. public interface SFilter {
  27.     public boolean test(String s);
  28.     public static String[] filter(String[] arr, SFilter filtr) {
  29.        
  30.         int count = 0;
  31.        
  32.         for(int i = 0; i < arr.length; i++) {
  33.             if(filtr.test(arr[i]))
  34.                 count++;
  35.         }
  36.         String[] wyn =new String[count];
  37.         count = 0;
  38.         for(int i = 0; i < arr.length; i++) {
  39.             if(filtr.test(arr[i])){
  40.                 wyn[count] = arr[i];
  41.                 count++;
  42.             }
  43.         }
  44.         return wyn;
  45.     }
  46. }
  47. public class LenFilter implements SFilter{
  48.    
  49.     private int minLen;
  50.    
  51.     public LenFilter(int minLen) {
  52.         this.minLen = minLen;
  53.     }
  54.     @Override
  55.     public boolean test(String s) {
  56.         return s.length() > minLen;
  57.     }
  58.    
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement