不多说,直接上干货!
package zhouls.bigdata.DataFeatureSelection.sim; public class test { public static int stringArrayCompare (String[] a, String[] b) { int num = 0; for (int i = 0; i < b.length; i++) { if (b[i].equals(a[i])) { num++; } else {//这里,控制从最左边开始,一旦遇到不同元素,则跳出 break; } } return num; } public static void main(String[] args) { String[] a = { "1","1","1","0" }; String[] b = { "1","1","1","1" }; System.out.println(stringArrayCompare(a, b)); } }
如果
String[] a = { "1","1","1","0" };
String[] b = { "1","1","0","0" };
则,结果也是2