简体   繁体   中英

How Do I compare two Arrays of Numbers and Link the result to a Button?

I'm creating a code for Basic operations with sets of numbers the intention is for the user to input the maximum amount of elements posible in two sets of numbers then the code generates the amount of Jtext blank spaces necessary for the given maximum number in each set.

Then I requested the system to print the numbers given by the user and now I want to be able to create a Button for the user that can compare both arrays already given by printing something like "Both Sets are equal".

So how do I compare the arrays already inputed in this case?

Thanks in advance ¡

public void actionPerformed(ActionEvent e1) {
        if(e1.getSource()==cubo){
            try{
                //Graphics g=getGraphics();
                op = Integer.parseInt(operando.getText());
                a = new JTextField [op];

                int i;

                for(i=0;i<op;i++){
                    a[i]=new JTextField(2);
                    panel1.add(a[i]);
                    a[i].setBounds(300,40+i*30, 50,20);
                    a[i].setText("");
                }

            }catch(NumberFormatException ex){
                operando.setText("error");

            }
        }
        if(e1.getSource()==leer){
            h=new int [op];
            int i;

            for(i=0;i<op;i++){
                int x = Integer.parseInt(a[i].getText());
                h[i]=x;
            }
            for(i=0;i<op;i++){
                System.out.println(h[i]);
            }
        }

        ////////////////////////////
        if(e1.getSource()==cubo1){
            try{
                //Graphics g=getGraphics();
                op1 = Integer.parseInt(operando.getText());
                a1 = new JTextField [op1];

                int i;

                for(i=0;i<op1;i++){
                    a1[i]=new JTextField(2);
                    panel1.add(a1[i]);
                    a1[i].setBounds(350,40+i*30, 100,20);
                    a1[i].setText("");
                }

            }catch(NumberFormatException ex){
                operando.setText("error");

            }
        }
        if(e1.getSource()==leer1){
            h1=new int [op1];
            int i;

            for(i=0;i<op1;i++){
                int x = Integer.parseInt(a1[i].getText());
                h1[i]=x;
            }
            for(i=0;i<op1;i++){
                System.out.println(h1[i]);
            }
        }

其中一个选项是将两个数组转换为Set并使用equals()方法比较它们,

new HashSet<int[]>(Arrays.asList(h)).equals(new HashSet<int[]>(Arrays.asList(h1)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM