Home java java. Comparison of arrays

java. Comparison of arrays

Author

Date

Category

Hello, tell me how can I compare 2 arrays? There is a code, in it 2 identical array, but the condition does not pass.

package vara;
Public Class Task4 {
  Public Static Void Main (String [] Args) {
    String [] password = {"q", "w", "e", "r", "t", "y"};
    If (Args.equals (Password))
      System.out.PrintLN ("Password is correct");
    ELSE {
      System.out.PrintLN ("Wrong Password");
    }
    for (int i = 0; i & lt; args.length; i ++)
      System.Out.print (Args [i]);
    System.Out.printLN ();
    For (int i = 0; i & lt; password.length; i ++)
      System.out.print (Password [i]);
  }
}

Answer 1, Authority 100%

array1.equals (array2) the same as array1 == array2 , addresses are compared, not content.

You need to use Arrays.equals (Array1, Array2) For comparison, i.e.

if (arrays.equals (args, password)) {...

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions