[英]How Do I compare two Arrays of Numbers and Link the result to a Button?
我正在创建一个基本操作的代码,其中包含一组数字,用户可以在两组数字中输入最大数量的元素,然后代码生成每个给定最大数量所需的Jtext空白数量。组。
然后我请求系统打印用户给出的数字,现在我希望能够为用户创建一个按钮,可以比较两个已经通过打印“两个集合都相等”的数组。
那么如何比较这种情况下已经输入的阵列呢?
提前致谢
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)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.