如何从两个textview中找到常用数字

时间:2017-04-07 03:04:37

标签: java android

我有两个Textview和两个按钮。

TextView TV1,TV2;
Button YES,NO;

现在

   TV1.setText("1 2 3 4 5");
   TV2.setText("3 4 5 6 7");

   YES.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

           //here i want to show common number that is (3,4,5)

            }
        });

    NO.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

    //here i want to show number  no matched in both Textview

           }
        }); 

现在我的问题是,当我点击它时,它显示为(3,4,5因为常见)FOR TV1,当我点击不显示(1,2,6,7)TV1时。

我可以通过设置文字来实现,但如果我有更多的数字会怎样。

3 个答案:

答案 0 :(得分:0)

您可以将号码放入ArrayList,以便于查找常用号码。

public List<Integer> findCommonElement(int[] a, int[] b) {

    List<Integer> commonElements = new ArrayList<Integer>();

    for(int i = 0; i < a.length ;i++) {
        for(int j = 0; j< b.length ; j++) {
            if(a[i] == b[j]) {
                //Check if the list already contains the common element
                if(!commonElements.contains(a[i])) {
                    //add the common element into the list
                    commonElements.add(a[i]);
                }
            }
        }
    }
    return commonElements;
}

您可以使用以下方式查看:

int myArray[] = { 2, 2, 7, 7, 2, 1, 5, 4, 5, 1, 1 };
int myArray2[] = { 2, 3, 4, 7, 10 };

Log.d("Common", String.valueOf(findCommonElement(myArray, myArray2)));

推介:here

答案 1 :(得分:0)

首先,你必须有一种方法让用户输入数字,如果你的意思是“如果我有更多的数字”是灵活性。

List<int> set1 = new ArrayList<int>();   //your set 1
List<int> set2 = new ArrayList<int>();   //your set 2
List<int> answer = new ArrayList<int>(); // container of common or difference

//假设你的集合1中已有一个值并设置为2

 YES.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            answer.clear() //always reset the List
            for(int i=0; i<set1.size(); i++)
            {
                for(int j=0; j<set2.size(); j++)
                {
                    if(set1.get(i)==set2.get(j))
                    {
                        answer.add(set2.get(j));
                        break;
                    }
                }
            }
            //you now have the value of all common @ this point
            //it's up to you what you will do now
        }
    });

 NO.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            answer.clear() //always reset the List
            int exist = 0;
            for(int i=0; i<set1.size(); i++)
            {
                for(int j=0; j<set2.size(); j++)
                {
                    if(set1.get(i)==set2.get(j))
                    {
                        exist += 1;
                    }
                }
                if(exist==0)
                {
                    answer.add(set1.get(i));
                }
                exist = 0;//reset
            }
            //you now have the value of all uncommon @ this point
            //it's up to you what you will do now
       }
    }); 

答案 2 :(得分:0)

试试这个 -

    TV1.setText("1 2 3 4 5");
    TV2.setText("3 4 5 6 7"); 
    String array1[]= TV1.getText().toString().split(" ");
    String array2[]= TV2.getText().toString().split(" ");
    List<Integer> aList = new ArrayList<>(Arrays.asList(array1));
    List<Integer> bList = new ArrayList<>(Arrays.asList(array2));
    List<Integer> union = new ArrayList(aList);
    union.addAll(bList);
    List<Integer> intersection = new ArrayList(aList);
    intersection.retainAll(bList);
    List<Integer> symmetricDifference = new ArrayList(union);
    symmetricDifference.removeAll(intersection);
    YES.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

       //here i want to show common number that is (3,4,5)
      System.out.println("common number: " + intersection);

        }
    });

    NO.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

      //here i want to show number  no matched in both Textview
        System.out.println("**symmetricDifference: " + symmetricDifference+"**");

       }
    }); 

打印:

 common number: [3, 4, 5]
 **symmetricDifference: [1, 2, 6, 7]**