简体   繁体   English

如何比较每个位置的两个数组字符串

[英]How to compare two arrays string for each position

I am new to java, and I have this problem; 我是java的新手,但是我有这个问题。 I am working with a webservice from android where I send a request and I get an answer formatted like this string: 1-0,2-0,3-0,4-0,5-0,6-0,7-0,8-0,12-0,13-0 where the number before the "-" means the number of my button and the number after "-" means the button status. 我正在使用来自android的网络服务,我在其中发送请求,并得到一个格式如下的答案: 1-0,2-0,3-0,4-0,5-0,6-0,7-0,8-0,12-0,13-0 ,其中“-”前面的数字表示我的按钮的编号,而“-”后面的数字表示按钮的状态。 I split this string into an array like this: 我将此字符串拆分为如下数组:

String  buttons = "1-0,2-0,3-0,4-0,5-0,6-0,7-0,8-0,13-0,14-0";
String[] totalButtons = buttons.split(",");

then I make a new request to get the status of my buttons and I get this 然后我提出一个新请求以获取按钮状态,我得到了

String status = "1-0,2-0,3-2,4-0,5-4,6-0,7-4,8-0,9-2,10-1,13-4,14-2";
String[] statusButtons = status.split(",");

The number of the buttons are going to be the same all time; 按钮的数量将一直保持不变; in this case 10 buttons. 在这种情况下,为10个按钮。 The problem that I have is how to compare each element of the two arrays if they can change their status every two seconds and I receive more buttons than the first time and I have to change their status with the new value. 我遇到的问题是,如果两个数组的每个元素每隔两秒钟就可以更改其状态,并且比第一次接收到的按钮更多,并且必须用新值更改其状态,则如何比较它们。 For example the first element of the array one is equal to the first element of the second array so there is no problem, but the first array does not have two elements in the second array in this case 9-2,10-1 so they should be deleted. 例如,数组的第一个元素等于第二个数组的第一个元素,因此没有问题,但是在这种情况下,第一个数组在第二个数组中没有两个元素9-2,10-1因此它们应该删除。 The final result should be like this 最终结果应该是这样的

String  buttons =     "1-0,2-0,3-0,4-0,5-0,6-0,7-0,8-0,13-0,14-0";
String status =       "1-0,2-0,3-2,4-0,5-4,6-0,7-4,8-0,9-2,10-1,13-4,14-2";
String finalButtons = "1-0,2-0,3-2,4-0,5-4,6-0,7-4,8-0,13-4,14-2";

Here's an idea to get you started; 这是一个让您入门的想法;

Map<String,String> buttonStatus = new HashMap<String,String>();

for (String button : totalButtons) {
    String parts[] = button.split("-");
    buttonStatus.put(parts[0], parts[1]);
}

for (String button : statusButtons) {
    String parts[] = button.split("-");
    if (buttonStatus.containsKey(parts[0])) {
        buttonStatus.put(parts[0], parts[1]);
    }
    // Java 8 has a "replace" method that will change the value only if the key
    // already exists; unfortunately, Android doesn't support it
}

The result will be a map whose keys are taken from the original totalButtons , and whose values will be taken from statusButtons if present. 结果将是一个映射,该映射的键从原始totalButtons ,并且其值将从statusButtons如果存在)。 You can go through the keys and values in the Map to get the results, but they won't be in order; 您可以遍历Map的键和值来获取结果,但是它们并没有顺序。 if you want them to be in the same order as totalButtons , go through totalButtons again and use buttonStatus.get to get each value. 如果希望它们与totalButtons顺序相同,请再次遍历totalButtons并使用buttonStatus.get获取每个值。

The javadoc for Map is here . Map的Javadoc在这里

I would split up each of those again and then compare those values. 我将再次拆分每个值,然后比较这些值。

ex: 例如:

String[] doubleSplit = totalButtons[index].split("-"); // "1-0" -> {"1", "0"}
import java.util.HashMap;
import java.util.Map;

/**
 * @author Davide
 */
public class test {
    static Map map;

    public static void main(String[] args) {
        // init value
        String buttons = "1-0,2-0,3-0,4-0,5-4,6-0,7-0,8-0,13-0,14-0";
        String[] keys = buttons.split("(-[0-9]*,*)");

        init(keys);

        // new value
        String status = "1-0,2-0,3-2,4-0,5-4,6-0,7-4,8-0,9-2,10-1,13-4,14-2";
        String[] statusButtons = status.split(",");

        update(statusButtons);
        print();
    }

    public static void init(String[] keys) {
        map = new HashMap<Integer, Integer>();
        for (String k : keys) {
            map.put(Integer.valueOf(k), 0);
        }
    }

    public static void update(String[] statusButtons) {
        for (String state : statusButtons) {
            String[] split = state.split("-");
            int k = Integer.valueOf(split[0]);
            int v = Integer.valueOf(split[1]);
            if (map.containsKey(k)) {
                map.put(k, v);
            }
        }
    }

    public static void print() {
        String out = "";
        for (Object k : map.keySet()) {
            out += k + "-" + map.get(k) + ",";
        }
        System.out.println(out.substring(0, out.length() - 1));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM