检查多个字符串是否包含字母的有效方法

时间:2017-11-26 12:27:52

标签: java

在下面的代码中,我将JButton及其标签传递给我的createKeyboard()方法。目标是以正确的顺序将JButtons添加到JPanel,以便它们以QWERTY键盘格式正确显示。按钮及其相应的标签将从A-Z开始传递给该方法。

此方法对字母进行排序并将其添加到正确的面板中。我想知道是否有一种更有效的方法,现在你可以看到我使用多个for循环来实现这一点,但我相信必须有更好的方法来做到这一点。

public void addKeyboard(char c, JButton button) {

    String QP = "QWERTYUIOP";
    String AL = "ASDFGHJKL";
    String ZM = "ZXCVBNM";

    keyboardQP = new ArrayList<JButton>(); //JButton ArrayLists
    keyboardAL = new ArrayList<JButton>();
    keyboardZM = new ArrayList<JButton>();




    for (int i = 0; i < QP.length(); i ++) {
        if (c == QP.charAt(i)) {
            keyboardQP.add(button);
        }
    }


    for (int i = 0; i < AL.length(); i ++) {
        if (c == AL.charAt(i)) {
            keyboardAL.add(button);
        }
    }

    for (int i = 0; i < ZM.length(); i ++) {
        if (c == ZM.charAt(i)) {
            keyboardZM.add(button);
        }
    }

5 个答案:

答案 0 :(得分:2)

您可以通过char方法找到indexOf的索引,然后将其添加到ArrayList根据索引。但是在实例化时需要事先设置列表大小。但它应该在构造函数中完成,因为每次在方法体内创建新的ArrayList是错误的,否则你将无法添加多个按钮。

String QP = "QWERTYUIOP";
keyboardQP = new ArrayList<>(QP.length());

int index;
if ((index = QP.indexOf(c)) >= 0) 
    keyboardQP.add(index, button);

在这种情况下,您不需要循环它。按钮将根据订单添加。

答案 1 :(得分:1)

你可以做的是创建一个布尔数组,索引是字符的int值(例如下面的例子),但是过早优化是所有邪恶的根源,for循环非常快,仅在需要时优化代码。代码可读性更重要的是使代码更好地维护并在数年后理解它。

示例:

static boolean[] qpArray = new boolean[127]; // do it outsite the 
// method, the jvm will not build the array every method call, when the 
// class will be initialized the static part will be executed.
static {
  Arrays.fill(qpArray, Boolean.FALSE);
  qpArray['Q'] = true;
  qpArray['W'] = true;
  qpArray['E'] = true;
  qpArray['R'] = true;
  qpArray['T'] = true;
  qpArray['Y'] = true;
}
// do the same for AL ZM...

public void addKeyboard(char c, JButton button) {
  final keyboardQP = new ArrayList<JButton>();

  if(qpArray[c]) {
    keyboardQP.add(c);
  }

  // do the same for AL ZM...
}

答案 2 :(得分:0)

我主要提供以下可读性代码

final List<String> QP = Arrays.asList("Q", "W", "E", "R", "T", "Y", "U", "I" , "O", "P");
final List<String> AL = Arrays.asList("A", "S", "D", "F", "G", "H", "J", "K", "L");

if(QP.contains(c)) {
    keyboardQP.add(button);
    break;
}

if(AL.contains(c)) {
    keyboardAL.add(button);
    break;
}

答案 3 :(得分:0)

制作自己的方法,并使用给定的参数调用它。 并改为调用此方法3次:)

public void adContent(String myString, ArrayList<JButton> myArrayList){

    for (int i = 0; i < myString.length(); i ++) {
        if (c == myString.charAt(i)) {
            myArrayList.add(button);
        }
    }
}

答案 4 :(得分:0)

微观优化,但不太可读的变体:

private static int[] mapping = new int[91];
static {
    mapping['Q'] = 1;
    mapping['W'] = 1;
    mapping['E'] = 1;
    mapping['R'] = 1;
    mapping['T'] = 1;
    mapping['Y'] = 1;
    mapping['U'] = 1;
    mapping['I'] = 1;
    mapping['O'] = 1;
    mapping['P'] = 1;
    mapping['A'] = 2;
    mapping['S'] = 2;
    mapping['D'] = 2;
    mapping['F'] = 2;
    mapping['G'] = 2;
    mapping['H'] = 2;
    mapping['J'] = 2;
    mapping['K'] = 2;
    mapping['L'] = 2;
    mapping['Z'] = 3;
    mapping['X'] = 3;
    mapping['C'] = 3;
    mapping['V'] = 3;
    mapping['B'] = 3;
    mapping['N'] = 3;
    mapping['M'] = 3;
}

private List<JButton> keyboardQP = new ArrayList<JButton>();
private List<JButton> keyboardAL = new ArrayList<JButton>();
private List<JButton> keyboardZM = new ArrayList<JButton>();

public void addKeyboard(char c, JButton button) {
    switch (mapping[c]) {
        case 1: 
            keyboardQP.add(button);
            break;
        case 2:
            keyboardAL.add(button);
            break;
        case 3:
            keyboardZM.add(button);
            break;
    }       

}