android compare arrays

纵然是瞬间 提交于 2019-12-20 01:37:29

问题


As I am not the best in android development, I tried something that works for me and for friend's mobile, but i have some reports from market that it doesn't work for all devices maybe and do wrong compare. Anyway. the project is simple, it grabs an order from sql and in the game the player try to finish it. So I have 2 arrays. I call this at start:

    final String[] combo = new String[] {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"}; 
    final String[] order1 = new String[] {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"};


    for(int i = 0; i < combo.length; i++) {
        combo[i] = new String();
        combo[i] = "0";
        order1[i] = new String();
        order1[i] = "0";
    }

and during the game if the player clicks a button it changes the value of combo, for example, combo[7] = "1";

When he click the final button I check that 2 arrays with this

String IsSame = compareOrder(combo, order1);

and then

   if (IsSame.equals("TRUE")) {

   // commands  

   }
   else if (IsSame.equals("FALSE")) {

   // commands                    

  }


private String compareOrder(String[] a, String[] b){
String n1 = "TRUE";
for (int i = 0; i < 13; i++) {
         if (a[i].equals(b[i])==false) {n1 = "FALSE";}
   }
return n1;  
}

It looks ok for me, and it's working for my mobile, but maybe the code is not so normal and it cause wrong results in other devices. So, I need help, if you see something strange and not working in my code, tell me. Thank you!


回答1:


Don't write what is provided already. :-)

import java.util.Arrays;

TextView tv = (TextView) findViewById(R.id.text_view);
tv.setText(Arrays.equals(order1, combo)? "Equal" : "Unequal");


来源:https://stackoverflow.com/questions/9710947/android-compare-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!