Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class StringFilter {
- public static void main(String[] args) {
- String[] arr = {"Alice", "Sue", "Janet", "Bea"};
- System.out.println(Arrays.toString(arr));
- String[] a1 = SFilter.filter(arr, new LenFilter(3));
- System.out.println(Arrays.toString(a1));
- String[] a2 = SFilter.filter(arr, new SFilter() {
- public boolean test(String s) {
- return (s.charAt(0) < 'D' && s.charAt(0) >='A');
- }
- });
- System.out.println(Arrays.toString(a2));
- String[] a3 = SFilter.filter(arr, s -> s.charAt(0) > 'H' && s.charAt(0) <= 'Z');
- System.out.println(Arrays.toString(a3));
- }
- }
- public interface SFilter {
- public boolean test(String s);
- public static String[] filter(String[] arr, SFilter filtr) {
- int count = 0;
- for(int i = 0; i < arr.length; i++) {
- if(filtr.test(arr[i]))
- count++;
- }
- String[] wyn =new String[count];
- count = 0;
- for(int i = 0; i < arr.length; i++) {
- if(filtr.test(arr[i])){
- wyn[count] = arr[i];
- count++;
- }
- }
- return wyn;
- }
- }
- public class LenFilter implements SFilter{
- private int minLen;
- public LenFilter(int minLen) {
- this.minLen = minLen;
- }
- @Override
- public boolean test(String s) {
- return s.length() > minLen;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement