1

Всем привет, как создать на вот этот код цикл? Я пробовал по такому типу

    for (int i = 0;i < 6;i++){
        boolean ai = checkWithRegExp(q1);
        boolean bi = checkWithRegExp1(q2);
    }

Но результаты безуспешны. Как в цикле задать, чтобы метод тоже менялся? Ставил checkWithRegExpi(qi) . Выдает ошибку.

    boolean a1 = checkWithRegExp(q1);
    boolean b1 = checkWithRegExp1(q2);

    boolean a2 = checkWithRegExp(w1);
    boolean b2 = checkWithRegExp1(w2);

    boolean a3 = checkWithRegExp(e1);
    boolean b3 = checkWithRegExp1(e2);

    boolean a4 = checkWithRegExp(r1);
    boolean b4 = checkWithRegExp1(r2);

    boolean a5 = checkWithRegExp(t1);
    boolean b5 = checkWithRegExp1(t2);
    //
    boolean a6 = checkWithRegExp(q1);
    boolean b6 = checkWithRegExp2(q2);

    boolean a7 = checkWithRegExp(w1);
    boolean b7 = checkWithRegExp2(w2);

    boolean a8 = checkWithRegExp(r1);
    boolean b8 = checkWithRegExp2(r2);

    boolean a9 = checkWithRegExp(e1);
    boolean b9 = checkWithRegExp2(e2);

    boolean a10 = checkWithRegExp(t1);
    boolean b10 = checkWithRegExp2(t2);
    //
    boolean a11 = checkWithRegExp(q1);
    boolean b11 = checkWithRegExp3(q2);

    boolean a12 = checkWithRegExp(w1);
    boolean b12 = checkWithRegExp3(w2);

    boolean a13 = checkWithRegExp(e1);
    boolean b13 = checkWithRegExp3(e2);

    boolean a14 = checkWithRegExp(r1);
    boolean b14 = checkWithRegExp3(r2);

    boolean a15 = checkWithRegExp(t1);
    boolean b15 = checkWithRegExp3(t2);
    //
    boolean a16 = checkWithRegExp(q1);
    boolean b16 = checkWithRegExp3(q2);

    boolean a17 = checkWithRegExp(w1);
    boolean b17 = checkWithRegExp3(w2);

    boolean a18 = checkWithRegExp(e1);
    boolean b18 = checkWithRegExp3(e2);

    boolean a19 = checkWithRegExp(r1);
    boolean b19 = checkWithRegExp3(r2);

    boolean a20 = checkWithRegExp(t1);
    boolean b20 = checkWithRegExp3(t2);
2
  • 14
    Не заводите миллиард переменных, если с ними нужно делать однотипные действия. Дочитайте учебник до массивов или (лучше) списков.
    – VladD
    11 сен 2017 в 21:13
  • Опишите логику ваших методов, как они должны меняться. Одинаковые ли переменные передаете в методы. Как определить какой метод вызывать? То, что написали вы зациклить невозможно, т.к. логика не циклична 12 сен 2017 в 0:59

1 ответ 1

1

Например, можно использовать списки:

public static void main(String[] args) {
    List<Pattern> patternList = Arrays.asList(Pattern.compile("reg1"), Pattern.compile("reg2"), Pattern.compile("reg3"), Pattern.compile("reg4"));
    List<String> dataList = Arrays.asList("str1", "str2", "str3", "str4");

    IntStream.range(0, patternList.size()).forEach(i -> System.out.println(checkWithRegExp(patternList.get(i), dataList.get(i))));
}

private static boolean checkWithRegExp(Pattern pattern, String data) {Matcher matcher = pattern.matcher(data);
    return matcher.find();
}
7
  • Автор вопроса ещё даже с массивами/списками толком не знаком. А у вас тут сразу и дженерики и Stream API. Подозреваю, что это слишком сложно.
    – Nick Volynkin
    12 сен 2017 в 4:11
  • @NickVolynkin а вдруг знаком и это вопрос касается архитектуры? Кстати, рад буду услышать варианты реализации. На ум приходит лямда. 12 сен 2017 в 4:37
  • «Cтавил checkWithRegExpi (qi) . Выдает ошибку.» Похоже, что не знаком. :)
    – Nick Volynkin
    12 сен 2017 в 4:39
  • @NickVolynkin может он генерирует сорцы в памяти, компилирует и исполняет?) 12 сен 2017 в 5:07
  • 1
    @Waluk мой монокль выпал на слове мистер :D 12 сен 2017 в 8:24

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.