zoukankan      html  css  js  c++  java
  • java中判断两个对象是否相等

    package ceshi.com.job;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class EqualTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String A = "ABba";
            String B = "ABbaa";
            
            if (A.equals(B)) {
                System.out.println("相等");
                
            }else
                System.out.println("不相等");
            
            System.out.println("-------------");
    //        String C = new String("hello");
            String C = "hello";
            String D = "hello";
            
            if (C==D) {
                System.out.println("相等");
                
            }else
                System.out.println("不相等");
            System.out.println("——————————————————————");
            String[] E = {"1","2","3","4"};
            String[] F = {"1","2","3","4"};
            
            if (Arrays.equals(E,F)) {
                System.out.println("相等");
            }else
                System.out.println("不相等");
            System.out.println("---------ooo");
            String[] G = {"1","2","3","4"};
            String[] H = {"1","2","3","4"};
            if (G.length==H.length) {
                for (int i = 0; i < G.length; i++) {
                    if (G[i].equals(H[i])) {
    //                    System.out.println("相等");
                    }else
                        System.out.println("不相等");
                }
                System.out.println("相等");
            }else{
                System.out.println("长不不相等,不需要判断");
            }
            System.out.println("-------------");
            
            List<String> one = Arrays.asList("Tom","Anthony","Beijing");
            List<String> two = Arrays.asList("Tom","Anthony","Beijing");
            
            if (one.containsAll(two)) {
                System.out.println("相等");
            }else{
                System.out.println("不相等");
                
            }
            System.out.println("-----------");
            List<String> str1 = Arrays.asList("Tom","Anthony");
            List<String> str2 = Arrays.asList("Tom");
            
            if (str1.size() == str2.size()) {
                for (int i = 0; i < str1.size(); i++) {
                    if (str1.get(i).equals(str2.get(i))) {
                        //do thing
                    }else{
                        System.out.println("不相同");
                    }
                }
                System.out.println("");
                
            }else{
                System.out.println("长度不相等,没有必要比较");
            }
        }
            
        
    }
  • 相关阅读:
    洛谷p1056
    __int64
    杭电2057
    4.4清北学堂Day1 主要内容:数论,数学
    递推的一点理解
    高精度减法
    高精度加法
    p1184高手之在一起
    对于rqy今天讲座的一些理解和看法吧
    PHP.21-商品信息管理
  • 原文地址:https://www.cnblogs.com/51testing/p/7742664.html
Copyright © 2011-2022 走看看