【问题标题】:How to display if those two arrays have the same number on the same place in both arrays?如果这两个数组在两个数组的同一位置具有相同的数字,如何显示?
【发布时间】:2019-05-22 15:57:33
【问题描述】:

所以这是我的问题: 我有两个数组,我想计算两个数组上完全相同的位置 [i] 上有多少值,以及两个数组上有多少值相同但不同的 [i]

我用 for 和数组的大小尝试了通常的循环,但它显示的奇怪值与预期的相差甚远

package stackOverflow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class mainStack extends JFrame implements ActionListener {

    JButton jbr,jbv,jbb,jbo,jbn,jbj; 
    JTextField l11b;
    String a;

    int tabRef[]= {0,1,2,3};
    int correctAndSameCase=0;
    int correctButDiffCase=0;
        mainStack(){

            this.setLayout(null);
            jbr = new JButton("Rouge");
            jbr.setBounds(0,80,85,30);
            add(jbr);

            jbv = new JButton("Vert");
            jbv.setBounds(125, 80, 85, 30);
            add(jbv);

            jbb = new JButton("Bleu");
            jbb.setBounds(0, 120, 85, 30);
            add(jbb);

            jbj = new JButton("Jaune");
            jbj.setBounds(125, 120, 85, 30);
            add(jbj);

            jbo = new JButton("Orange");
            jbo.setBounds(0, 160, 85,30);
            add(jbo);

            jbn = new JButton("Noir");
            jbn.setBounds(125,160, 85,30);
            add(jbn);

            jbr.addActionListener(this);
            jbv.addActionListener(this);
            jbb.addActionListener(this);
            jbj.addActionListener(this);
            jbo.addActionListener(this);
            jbn.addActionListener(this);

            setLayout(null);
            setSize(800,800);
            setVisible(true);
        }
        private int index = 0;
        private int p=0;
        private int tabAnswer[][] = new int[3][4];
        private int i;


        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(jbr)) {
                tabAnswer[p][index] = 0;
            } else if (e.getSource().equals(jbv)) {
                tabAnswer[p][index] = 1;
            } else if (e.getSource().equals(jbj)) {
                tabAnswer[p][index] = 2;
            } else if (e.getSource().equals(jbb)) {
                tabAnswer[p][index] = 3;
            } else if (e.getSource().equals(jbo)) {
                tabAnswer[p][index] = 4;
            } else if (e.getSource().equals(jbn)) {
                tabAnswer[p][index] = 5;
            }
            index++;
            if (index >= tabAnswer[p].length) {
                System.out.println(Arrays.toString(tabAnswer[p]));

                for(i=0 ; i<tabRef.length;i++) {

                    if(tabAnswer[p][i]==tabRef[i]) {

                        correctAndSameCase++;
                    }else if(tabAnswer[p][i]==tabRef[0] & tabAnswer[p][i]!=tabRef[i]  ||  tabAnswer[p][i]==tabRef[1] & tabAnswer[p][i]!=tabRef[i] || tabAnswer[p][i]==tabRef[2]& tabAnswer[p][i]!=tabRef[i] ||tabAnswer[p][i]==tabRef[3] & tabAnswer[p][i]!=tabRef[i]) {

                        correctButDiffCase++;
                    }

                }
                index = 0;
                p++;
                System.out.println(correctAndSameCase+" number are on the same case on both arrays");
                System.out.println(correctButDiffCase+" number are on different case on both arrays");
                if(p>=3) {
                    p=0;
                }
                correctAndSameCase=0;
                correctButDiffCase=0;

            }
        }
    public static void main(String[] args) {
        mainStack t= new mainStack();
}

这里 tabAnswer {5,4,3,2} correctAndSameCase 应该是 0 和 correctButDiffCase 应该是 2 但它给了我 0 和 1 作为答案。

编辑:我可以看到问题在于将值设置为correctButDiffCase但我不知道如何解决它

【问题讨论】:

    标签: java arrays comparison


    【解决方案1】:

    这里有一个短代码 sn-p 计算这两个值。 但我不知道,如果您想计算一些特殊情况(请参阅 TODO)。

        public static void main(String[] args) throws Exception {
    
        List<Integer> tabRef =  Arrays.asList(10, 5, 3, 5, 2, 6);
        List<Integer> tabRef2 = Arrays.asList(12, 8, 3, 5, 6, 2);
    
        int matchesOnSameIndex = 0;
        int matchesButDifferentIndex = 0;
    
        for (int i = 0; i < tabRef.size(); i++) {
            //compare on same index, if other list has element on this index
            if (tabRef2.size() > i) {
                if (tabRef.get(i) == tabRef2.get(i)) {
                    //same element on same index
                    matchesOnSameIndex++;
                }
            }
    
            //check if value exists in other list
            if (tabRef2.contains(tabRef.get(i))) {
                //if yes, check if it is not at same position
                if (tabRef2.size() > i) {
                    if (tabRef2.get(i) != tabRef.get(i)) {
                        //not same index
                        //TODO must count multiple times, if element exists multiple times?
                        matchesButDifferentIndex++;
                    }else {
                        //TODO must count, if also exists on other index?
                    }
                }else {
                    //same position not existing, so must be on other position
                    matchesButDifferentIndex++;
                }
            }
        }
    
        System.out.println("matchesOnSameIndex: "+matchesOnSameIndex);
        System.out.println("matchesButDifferentIndex: "+matchesButDifferentIndex);
    
    }
    

    【讨论】:

      【解决方案2】:

      您计算出现次数的代码看起来不错(虽然有点乱)。 我用你的代码写了一个junit测试,没有问题:

        @Test
        public void testArrayCheck() {
          int[] tabRef = {0,1,2,3};
          int tabAnswer[][] = new int[3][4];
          int[] tabAnswerPos0 = {0,0,0,0};
          tabAnswer[0] = tabAnswerPos0;
          int p = 0;
          int correctAndSameCase = 0;
          int correctButDiffCase = 0;
          Set<Integer> setArrayValues = new HashSet<Integer>();
          for(int i=0 ; i<tabRef.length;i++) {
            if(tabAnswer[p][i]==tabRef[i]) {
                correctAndSameCase++;
            }else if(tabAnswer[p][i]==tabRef[0] && tabAnswer[p][i]!=tabRef[i]  
                ||  tabAnswer[p][i]==tabRef[1] && tabAnswer[p][i]!=tabRef[i] 
                    || tabAnswer[p][i]==tabRef[2] && tabAnswer[p][i]!=tabRef[i] 
                        ||tabAnswer[p][i]==tabRef[3] && tabAnswer[p][i]!=tabRef[i]) {
                if (!setArrayValues.contains(tabAnswer[p][i])) {
                  correctButDiffCase++;
                  setArrayValues.add(tabAnswer[p][i]);
                }
            }
          }
        System.out.println(correctAndSameCase+" number are on the same case on both arrays");
        System.out.println(correctButDiffCase+" number are on different case on both arrays");
        }
      
      1 number are on the same case on both arrays
      1 number are on different case on both arrays
      

      您确定 tabAnswers 的值为 {5,4,3,2}?

      【讨论】:

      • 我在代码之前搞砸了,所以这在我身上,但我现在真正的问题是我的tabAnswer {0,0,0,0}tabRef的数量是否有多次,它应该只算作一次,但是计为多个我怎样才能让它只计为一个?
      • 您可以添加一个集合,在其中添加您已经检查过的值,如果它已经存在,则不要增加变量。我为您的新请求编辑了答案
      猜你喜欢
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      相关资源
      最近更新 更多