Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9081203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:15:35+00:00 2026-06-16T20:15:35+00:00

Check to see if the array arr1 contain the same elements as arr2 in

  • 0

Check to see if the array arr1 contain the same elements as arr2 in the same order in java.

for example:

    isTheSame({"1", "2", "3"}, {"1", "2", "3"}) → true
    isTheSame({"1", "2", "3"}, {"2", "1", "1"}) → false
    isTheSame({"1", "2", "3"}, {"3", "1", "2"}) → false

so far i have

public boolean isTheSame(String[] arr1, String[] arr2)
{
   if (arr1.length == arr2.length)
   {
      for (int i = 0; i < arr1.length; i++)
       {
          if (arr1[i] == arr2[i])
          {
            return true;
          }
       }
    }
    return false;  
 }

The problem with this is that it only compares the first element of the two arrays.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:15:36+00:00Added an answer on June 16, 2026 at 8:15 pm

    You are iterating until you find a match. You should instead be looking for a String which doesn’t match and you should be using equals not ==

    // same as Arrays.equals()
    public boolean isTheSame(String[] arr1, String[] arr2) {
        if (arr1.length != arr2.length) return false;
        for (int i = 0; i < arr1.length; i++)
            if (!arr1[i].equals(arr2[i]))
                return false;
        return true;
    }
    

    FYI This is what Arrays.equals does as it handle null values as well.

    public static boolean equals(Object[] a, Object[] a2) {
        if (a==a2)
            return true;
        if (a==null || a2==null)
            return false;
    
        int length = a.length;
        if (a2.length != length)
            return false;
    
        for (int i=0; i<length; i++) {
            Object o1 = a[i];
            Object o2 = a2[i];
            if (!(o1==null ? o2==null : o1.equals(o2)))
                return false;
        }
    
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to check to see if an array has - to activate a
SO I wish to check to see if the item in my array [clientDataArray
If I've got an array of strings, can I check to see if a
I have an array named $uid. How can I check to see how many
So I am trying to check to see if this array has an underscore
How does one loop through an array and then subsequently check to see if
How can I check to see if an element in an array is empty
I want to check to see if within the current position of an array
I have an array of values that I want to check to see if
Possible Duplicate: More concise way to check to see if an array contains only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.