5 Replies - 379 Views - Last Post: 25 November 2012 - 08:50 PM Rate Topic: -----

#1 NearbyBridge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-November 12

Program that displays whether two lists of integers identical or not

Posted 25 November 2012 - 08:00 PM

Im trying to have my program return that the arrays are identical if they have the same contents. I'm really quite stuck and I cant identify the problem. No matter what I enter for the arrays it continues to return "The two arrays are not identical. If anyone can spot what I'm doing wrong please let me know. Thank you

The input is:

list1: 51 25 22 6 1 4 24 54 6
list2: 51 22 25 6 1 4 24 54 6

Here is my code

import java.util.Scanner;


public class Identical Arrays {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        final int row = 3;
        final int column = 3;
        System.out.print("Enter list1: ");
        int[][] m1 = new int[row][column];
        for (int i = 0; i < m1.length; i++) {
            for (int j = 0; j < m1[0].length; j++) {
                m1[i][j] = input.nextInt();
            }
        }

        System.out.print("Enter list2: ");
        int[][] m2 = new int[row][column];
        for (int i = 0; i < m2.length; i++) {
            for (int j = 0; j < m2[0].length; j++) {
                m2[i][j] = input.nextInt();
            }
        }


        if (equals(m1, m2)) {
            System.out.println("The two arrays are identical");
        } else {
            System.out.println("The two arrays are not identical");
        }
    }

    public static boolean equals(int[][] m1, int[][] m2) {
        
        if (m1.length == m2.length) {

        
     
      double[][] result = new double[m1.length][m1[0].length];
  	
  	// Copy from m to result
  	for (int i = 0; i < m1.length; i++)
      for (int j = 0; j < m1[0].length; j++)
        result[i][j] = m1[i][j];

  	// Check the sum of each column
  	for (int i = 0; i < result.length; i++) 
  		java.util.Arrays.sort(result[i]);

            // Don't return quite yet

        double[][] result2 = new double[m2.length][m2[0].length];
  	
  	// Copy from m to result
  	for (int i = 0; i < m2.length; i++)
      for (int j = 0; j < m2[0].length; j++)
        result[i][j] = m2[i][j];

  	// Check the sum of each column
  	for (int i = 0; i < result.length; i++) 
  		java.util.Arrays.sort(result[i]);
        // Now test if every element is the same
        }
      
 else

        return false;   


        for (int i = 0; i < m1.length; i++) {
           
            if (m1[i] != m2[i])

            return false; 

        } 
        return true;
    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Program that displays whether two lists of integers identical or not

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Program that displays whether two lists of integers identical or not

Posted 25 November 2012 - 08:18 PM

Gheeezz you made it a lot complicated for nothing

public boolean equals(int[][] a1, int[][] a2) {
   if(a1.length != a2.length)
      return false;

   for(int row = 0; row < a1.length; ++row) {
      if(a1[row].length != a2[row].length)
         return false;
      for(int col = 0; col < a1[row].length; ++col) {
          if(a1[row][col] != a2[row][col])
             return false;
   }
   return true;  // Hey I pass over all tests :)/>/>
}


Happy coding :^:/>

or

private boolean equals(int[] a1, int[] a2) {
    if(a1.length != a2.length)
       return false;
    for(int i = 0; i < a1.length; ++i) {
       if(a1[i] != a2[i])
           return false;
    }
    return true;
}

public boolean equals(int[][] a1, int[][] a2) {
   if(a1.length != a2.length)
      return false;

   for(int row = 0; row < a1.length; ++row) {
      if(!equals(a1[row], a2[row])
         return false;
   }
   return true;  // Hey I pass over all tests :)/>/>
}


This post has been edited by pbl: 25 November 2012 - 08:22 PM
Reason for edit:: Added a better solution

Was This Post Helpful? 1
  • +
  • -

#3 NearbyBridge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-November 12

Re: Program that displays whether two lists of integers identical or not

Posted 25 November 2012 - 08:23 PM

I changed it a little bit and I think this is a little better but it's still not quite right because it returns identical for:

51 5 22 6 1 4 24 54 6
51 22 25 6 1 4 24 54 6

but it should return identical

import java.util.Scanner ;

public class Exercise07_29 {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    
    final int row = 3;
    final int column = 3;
    System.out.print("Enter list1: ");
    int[][] m1 = new int[row][column];    
    for (int i = 0; i < m1.length; i++)
      for (int j = 0; j < m1[0].length; j++)
        m1[i][j] = input.nextInt();

    System.out.print("Enter list2: ");
    int[][] m2 = new int[row][column];    
    for (int i = 0; i < m2.length; i++)
      for (int j = 0; j < m2[0].length; j++)
        m2[i][j] = input.nextInt();
    

    if (equals(m1, m2))
    	System.out.println("The two arrays are identical");
    else
    	System.out.println("The two arrays are not identical");
  }
  
  public static boolean equals(int[][] m1, int[][] m2) {
  	if (m1.length * m1[0].length != m2.length * m2[0].length)
  		return false;
  	
  	for (int i = 0; i < m1.length; i++)
  		for (int j = 0; j < m1[0].length; j++)
  			if (m1[i][j] != m2[i][j])
  				return true;
  		
    return true;
  } 
}



WOW that is so much since!. I feel like a clutz. Thank you so much.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Program that displays whether two lists of integers identical or not

Posted 25 November 2012 - 08:24 PM

if (m1.length * m1[0].length != m2.length * m2[0].length)

will you tell me what yu are trying to do ???
Was This Post Helpful? 0
  • +
  • -

#5 NearbyBridge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-November 12

Re: Program that displays whether two lists of integers identical or not

Posted 25 November 2012 - 08:27 PM

I was trying to make it return false if the lengths of the two arrays were not equal
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Program that displays whether two lists of integers identical or not

Posted 25 November 2012 - 08:50 PM

will simply return in the number of rows are the same and if the number of colums of the first row is the same...

and this is not even true
if array1 number of rows is 10 and first row number of columns is 5
and array2 number of rows is 5 and number of columns is 10
it will return true :)

better to use my version :)

This post has been edited by pbl: 25 November 2012 - 08:52 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1