我如何才能在数组中写入不同或缺少的元素的输出?

时间:2019-05-13 17:20:03

标签: java arrays

运行此命令时,我可以看到准确的结果“ false”,但我也想看看有什么区别。例如

  

“奥泽尔和古妮”相匹配,但“奥本与阿迪”则不匹配

package start;

public class Start {
    public static void main(String[] args) {


        String[] a = {"ozer + gunthy + albundy"};
        String[] b = {"ozer + günthy + aldy"};

        boolean b1 = false;

        if (a.length != b.length){
            b1 = false;
        }else {
            for (int i = 0; i < a.length; i++){
                if (a[i] == b[i]){
                    b1 = true;
                }else {
                    b1 = false;
                    break;
                }
            }
        }

        System.out.println(b1);
    }
}

1 个答案:

答案 0 :(得分:0)

您将要替换

  

a[i] == b[i]

a[i].equals(b[i])

您还需要修复阵列

public static String[] list1 = {"ozer", "gunthy","albundy"};
public static String[] list2 = {"ozer", "günthy","aldy"};

然后,您将需要编写并调用一个方法以根据您的规范正确分析数组:

public String methodName(String[] list1, String[] list2)
{
    String output = "";
    if (list1.length != list2.length)
    {
        return "The Arrays differ in length";
    }
    else
    {
        for (int i = 0; i < list1.length; i++)
        {
            if (list1[i].equals(list2[i]))
            {
                output += "" + list1[i] + " is equal to " + list2[i] + "\n";
            }
            else 
            {
                output += "" + list1[i] + " is not equal to " + list2[i] + "\n";
            }
        }
    }

    return output;
}