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 7047063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:43:31+00:00 2026-05-28T02:43:31+00:00

I´m trying to find similar rows in multiple two-dimensional arrays as it was described

  • 0

I´m trying to find similar rows in multiple two-dimensional arrays as it was described in my previous post. For the below-given example, the answer is false, true, although it should be false, false.

Another very important question is how to adjust this code to arrays with the different number of rows.

I appreciate very much any help. Thanks.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Test {

    public static void main(String[] args) {
        ArrayList<Integer[]> array1 = new ArrayList<Integer[]>(); 
        ArrayList<Integer[]> array2 = new ArrayList<Integer[]>();
        ArrayList<Integer[]> array3 = new ArrayList<Integer[]>();
        array1.add(new Integer[]{1,2,3}); array1.add(new Integer[]{1,0,3});
        array2.add(new Integer[]{1,0,3}); array2.add(new Integer[]{0,0,3});
        array3.add(new Integer[]{1,2,3}); array3.add(new Integer[]{0,3,3});
        for (int i=0; i<array1.size(); i++) {
            boolean answ = equalRows(array1.get(i),array2.get(i),array3.get(i));
            System.out.println(answ);
        }
    }
    
    static class Row extends Object {
        private int value;
        
        public Row(int val) {
            this.value = val;
        }
        
        @Override
        public boolean equals(Object obj) {
            if(this == obj)
                return true;
            if((obj == null) || (obj.getClass() != this.getClass()))
                return false;
            // object must be Row at this point
            Row row = (Row)obj;
                return (value == row.value);
        }
        
        @Override
        public int hashCode () {
            return this.value;
        }
    }

    private static Map<Row, Integer> map(Integer[] row) {
          Map<Row, Integer> rowMap = new HashMap<Row, Integer>();
          for (int i=0; i<row.length; i++)
              rowMap.put(new Row(row[i]), i);
          return rowMap;
    }

    private static boolean equalRows(Integer[] row1, Integer[] row2, Integer[] row3){
           Map<Row, Integer> map1 = map(row1);
           Map<Row, Integer> map2 = map(row2);

           for (int i=0; i<row3.length; i++){
              Row row = new Row(row3[i]);
              Integer result1 = map1.get(row);
              Integer result2 = map2.get(row);
              if (result1 == null || result2 == null) {
                  return false;
              }
           }
        return true;
    }

}

Edit#1
In the first test I´m comparing {1,2,3}, {1,0,3} and {1,2,3}. In the second: {1,0,3}, {0,0,3}, {0,3,3}. The problem with the second row is that {0,0,3} and {0,3,3} are tackled as {0,3}. I don´t know how to modify the code to deferentiate between {0,0,3} and {0,3,3} (I still should use HashMap).

Edit#2
The idea is that first I take rows from array1 and array2 and I put them into maps. Then I take a row from array3 and try to find it in maps. If I can´t find it in any of these maps, then it means that rows are not similar.

  • 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-05-28T02:43:32+00:00Added an answer on May 28, 2026 at 2:43 am

    To compare two arrays, ignoring nulls you can have

    public static <T> boolean equalsExceptForNulls(T[] ts1, T[] ts2) {
        if (ts1.length != ts2.length) return false;
        for(int i = 0; i < ts1.length; i++) {
           T t1 = ts1[i], t2 = ts2[i];
           if (t1 != null && t2 != null && !t1.equals(t2)) 
               return false;
        }
        return true;
    }
    
    public static <T> boolean equalsExceptForNulls3(T[] ts1, T[] ts2, T[] ts3) {
        return equalsExceptForNulls(ts1, ts2) && 
               equalsExceptForNulls(ts1, ts3) && 
               equalsExceptForNulls(ts2, ts3);
    }
    // or generically
    public static <T> boolean equalsExceptForNulls(T[]... tss) {
        for(int i = 0; i < tss.length - 1; i++)
           for(int j = i + 1; i < tss.length; j++)
               if(!equalsExceptForNulls(tss[i], tss[j]) 
                   return false;
        return true;
    }
    

    The problem you have is that array3 is being used to determine which rows to compare.

    In the first test you are comparing rows 1,2,3 and the second test you are comparing rows 0 and 3. The first test should be false and the second should be true.

    I found the issue by stepping through your code in a debugger. I suggest you do the same.

    I would also use int[] instead of Integer[]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find out how similar these two loops are when compiled: for
I'm trying to find similar or equivalent function of Matlabs Bwareaopen function in OpenCV?
I am trying to make a program that will find similar images from a
Trying to find an example that has css rollover using sprites & sliding door
Trying to find a way to send a POST HTTPS request from Python to
I'm trying to join multiple myqsl tables and then process resulting arrays using PHP
Couldn't find similar enough question so I'll ask. I'm trying to use facebook log-in
I am trying to find similar strings in feedback option texts using regex to
I'm trying to write a SQL statement to find all of the rows in
I´m trying to write the code for the problem described in my previous topic

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.