[英]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.