3
\$\begingroup\$

I have the following arrays which save the MAC-adresses of a lot of bluetooth beacons and their positions in a building:

private static final String[][] MAC_ADDRESSES_A_BLUETOOTH = {{"B09122F5D126", "210.105"}, {"B09122F5F26B", "136.121"}, {"B09122F5F619", "181.172"}};
...
private static final String[][] MAC_ADDRESSES_Q_BLUETOOTH = {{"B09122F5D124", "210.105"}, {"B09122F5F26Z", "136.121"}, {"B09122F5F61Q", "181.172"}};

Then I'm looking for the matching triple based on a current triple:

private String[][] compareArrays(String[] beacon) {
        if (Arrays.equals(beacon, getFirstArray(MAC_ADDRESSES_A_BLUETOOTH))) {
            return MAC_ADDRESSES_A_BLUETOOTH;
        } else if (Arrays.equals(beacon, getFirstArray(MAC_ADDRESSES_B_BLUETOOTH))) {
            return MAC_ADDRESSES_B_BLUETOOTH;
        } else if (Arrays.equals(beacon, getFirstArray(MAC_ADDRESSES_C_BLUETOOTH))) {
            return MAC_ADDRESSES_C_BLUETOOTH;
        } else if (Arrays.equals(beacon, getFirstArray(MAC_ADDRESSES_D_BLUETOOTH))) {
            return MAC_ADDRESSES_D_BLUETOOTH;
        } else if (Arrays.equals(beacon, getFirstArray(MAC_ADDRESSES_F_BLUETOOTH))) {
            return MAC_ADDRESSES_F_BLUETOOTH;
        } else if (Arrays.equals(beacon, getFirstArray(MAC_ADDRESSES_G_BLUETOOTH))) {
            return MAC_ADDRESSES_G_BLUETOOTH;
        } else if (Arrays.equals(beacon, getFirstArray(MAC_ADDRESSES_H_BLUETOOTH))) {
            return MAC_ADDRESSES_H_BLUETOOTH;
        } else if (Arrays.equals(beacon, getFirstArray(MAC_ADDRESSES_I_BLUETOOTH))) {
            return MAC_ADDRESSES_I_BLUETOOTH;
        } ...
        } else {
            return null;
        }
}

This code looks horrible and it bugs me. Is there an easier way to do this without having to write 16 if statements? getFirstArray returns all first values from a two-dimensional array.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

For readability, use an Object instead of a 2d array.

E.G:

public class BluetoothMacAddress {
    private String macAddress;
    private float positionInBuilding;
}

Use java.util.List, then you can have a list of lists:

private sttaic final List<MacAddress> MAC_ADDRESSES_A_BLUETOOTH = Arrays.asList(
    new BluetoothMacAddress("B09122F5D126", "210.105"),  
    new BluetoothMacAddress("B09122F5F26B", "136.121"), 
    new BluetoothMacAddress("B09122F5F619", "181.172"));

private static final List<List<MAC_ADDRESSES>> MAC_ADDRESSES = Arrays.asList(
    MAC_ADDRESSES_A_BLUETOOTH,
    MAC_ADDRESSES_B_BLUETOOTH 
    ...
);

Your new method would then look something like this:

private MacAddress compareArrays(String[] beacon) {
    for (List<MacAddress> macAddresses : MAC_ADDRESSES ) {
        if (Arrays.equals(beacon, getFirstArray(macAddresses))) {
            return macAddresses;
        }
    }
    // not found
    return null;
}

You'd have to change the beacon parameter to be a MacAddress too, or convert the MacAddress to a String[] (or visa-versa).

\$\endgroup\$
1
  • \$\begingroup\$ Used your solution, needed to change a lot in my other code, but looks cleaner in the end. \$\endgroup\$ Commented Nov 27, 2019 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.