2017-04-07 27 views
-4

找到共同的數量有兩個TextView的和兩個按鈕。我如何從兩個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因爲常見)TV1,當我點擊不顯示(1,2,6,7)TV1。

我可以集文字,但怎樣做,如果我有更多的數字。

+0

請更改標題更多的東西 –

+0

請建議任何其他 –

+0

只針對您的問題。標題=問題。 – jace

回答

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

+0

這是第一步......什麼是完整的答案? –

+0

@ cricket_007結賬編輯 –

+0

好吧,這是常見的元素,但問題還要求相差太大 –

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 
     } 
    }); 
+0

,如果我在這裏有一個錯誤,隨意評論:)我可能包括C#編碼在這裏的java:d – jace

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]**