differentequals.java

来自「java编程代码」· Java 代码 · 共 57 行

JAVA
57
字号

public class DifferentEquals
{
    /**
     A demonstration to see how == and an equalArrays method are different.
    */
    public static void main(String[] args)
    {
        int[] c = new int[10];
        int[] d = new int[10]; 

        int i;
        for (i = 0; i < c.length; i++)
            c[i] = i;

        for (i = 0; i < d.length; i++)
            d[i] = i;

        if (c == d)
            System.out.println("c and d are equal by ==.");
        else
            System.out.println("c and d are not equal by ==."); 
 
        System.out.println("== only tests memory addresses.");

        if (equalArrays(c, d))
            System.out.println(
                   "c and d are equal by the equalArrays method.");
        else
            System.out.println(
                  "c and d are not equal by the equalArrays method.");

        System.out.println(
                "An equalArrays method is usually a more useful test.");

    }

   public static boolean equalArrays(int[] a, int[] b)
   {
       if (a.length != b.length)
           return false;
       else
       {
           int i = 0;
           while (i < a.length)
           {
               if (a[i] != b[i])
                   return false;
               i++;
           }
       }
 
       return true;
    }
 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?