比较在Java中两个整型数组 [英] Comparing two integer arrays in java

查看:117
本文介绍了比较在Java中两个整型数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写code比较两个数组。在第一个阵列我已经把我自己的数字,但第二阵列从输入文件中获取数字。这种阵列的尺寸由该文件中的第一个数字,而第一阵列总是尺寸10的长度必须为两个阵列以及数字相同​​的决定。我的code是如下:

 公共静态无效compareArrays(INT []数组1,INT []数组2){
    布尔B = FALSE;
    的for(int i = 0; I< array2.length;我++){        对于(int类型的= 0; A< array1.length; A ++){            如果(数组2 [I] == ARRAY1 [一个]){
                B =真实的;
                的System.out.println(真);
            }其他{
                B = FALSE;
                的System.out.println(假);
                打破;
            }
        }
    }
}


解决方案

 公共静态无效compareArrays(INT []数组1,INT []数组2){
        布尔B =真实的;
        如果(数组1 = NULL&放大器;!&安培;!数组2 = NULL){
          如果(array1.length!= array2.length)
              B = FALSE;
          其他
              的for(int i = 0; I< array2.length;我++){
                  如果(数组2 [I]!=数组1 [I]){
                      B = FALSE;
                  }
            }
        }其他{
          B = FALSE;
        }
        的System.out.println(B);
    }

I am trying to write code to compare two arrays. In the first array I have put my own digits, but the second the array takes numbers from the input file. The size of this array is determined by the first number in the file while the first array is always of size 10. The length must be the same for both arrays as well as the numbers. My code is below:

public static void compareArrays(int[] array1, int[] array2) {
    boolean b = false;
    for (int i = 0; i < array2.length; i++) {

        for (int a = 0; a < array1.length; a++) {

            if (array2[i] == array1[a]) {
                b = true;
                System.out.println("true");
            } else {
                b = false;
                System.out.println("False");
                break;
            }
        }
    }       
}

解决方案

public static void compareArrays(int[] array1, int[] array2) {
        boolean b = true;
        if (array1 != null && array2 != null){
          if (array1.length != array2.length)
              b = false;
          else
              for (int i = 0; i < array2.length; i++) {
                  if (array2[i] != array1[i]) {
                      b = false;    
                  }                 
            }
        }else{
          b = false;
        }
        System.out.println(b);
    }

这篇关于比较在Java中两个整型数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆