Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android compare arrays

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!

like image 294
user1234189 Avatar asked Dec 12 '22 04:12

user1234189


1 Answers

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");
like image 87
Sparky Avatar answered Jan 13 '23 09:01

Sparky