Reputation: 133
In the below code I am passing a JButton and it's label into my createKeyboard()
method. The goal is to add the JButtons to a JPanel in the correct order so that they display correctly in a QWERTY keyboard format. The buttons and it's corresponding label will be passed to this method starting from A - Z.
This method sorts the letters and adds it to the correct panel. I'd like to know if there is a more efficient way of doing this, right now as you can see I am using multiple for loops to achieve this but I am sure there must be a better way of doing this.
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);
}
}
Upvotes: 1
Views: 89
Reputation: 18792
A simple Java 8 alternative for constructing the array lists:
String AL = "ASDFGHJKL";
List<JButton> keyboardAL = AL.chars()
.mapToObj(i -> (char)i)
.map(ch -> String.valueOf(ch))
.map(JButton::new)
.collect(Collectors.toList());
This should be done once, at class level, and not with each method invocation.
Upvotes: 0
Reputation: 498
A quite readable, although not micro-optimized variant:
private static final String QP = "QWERTYUIOP";
private static final String AL = "ASDFGHJKL";
private static final String ZM = "ZXCVBNM";
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) {
String s = String.valueOf(c);
if (QP.contains(s)) {
keyboardQP.add(button);
} else if (AL.contains(s)) {
keyboardAL.add(button);
} else if (ZM.contains(s)) {
keyboardZM.add(button);
}
}
Upvotes: 0
Reputation: 498
A micro-optimized, although not too readable variant:
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;
}
}
Upvotes: 0
Reputation: 1396
You may find index of the char
by indexOf
method and then add it to the ArrayList
according index. But you need to set list size beforehand when you instantiate it. But it should be done in constructor, because it is wrong to create new ArrayList
each time inside method's body, otherwise you won't be able to add more than one button.
String QP = "QWERTYUIOP";
keyboardQP = new ArrayList<>(QP.length());
int index;
if ((index = QP.indexOf(c)) >= 0)
keyboardQP.add(index, button);
In this case you don't need to loop it. And buttons will be added according their order.
Upvotes: 2
Reputation: 326
make your own method, and call it with the given parameters. and call this method 3 times instead :)
public void adContent(String myString, ArrayList<JButton> myArrayList){
for (int i = 0; i < myString.length(); i ++) {
if (c == myString.charAt(i)) {
myArrayList.add(button);
}
}
}
Upvotes: 0
Reputation: 1287
What you can do is create a boolean array which the index is the int value of the character (example below), but premature optimization is the root of all evil, the for loops are very fast, optimize the code only when it's needed. code readability is more important that makes the code better to maintain and to understand it after years.
Example:
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...
}
Upvotes: 1
Reputation: 3686
I'd rather have the following code for readability mainly
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;
}
Upvotes: 0