Results 1 to 3 of 3
  1. #1
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Compare 2 2D arrays and Conditionally Append Data

    Hi All,

    I'm working with some sample code to test and comprehend the logic necessary to achieve my objective in a real JAVA application.

    I've got 2 2D arrays of different, but uniform sizes. I want to compare array1[0][i] to array2 [1][d]. If they match, I want to insert that row from array2 into array1[5-7][i]. To illustrate if I have:

    array1:
    ABC 123 456 456 456 null null null
    DEF 789 111 456 456 null null null
    GHI 444 555 456 456 null null null
    XYZ 5AG 78R 456 456 null null null

    array2:
    123 ABC 456
    000 XYZ 111
    GHI 444 555

    I want array1 to contain this after I'm done:
    ABC 123 456 456 456 123 ABC 456
    DEF 789 111 456 456 null null null
    GHI 444 555 456 456 null null null
    XYZ 5AG 78R 456 456 000 XYZ 111

    I'm not receiving errors of any kind, but when I test print array1 a second time, it contains it's original contents, not as I've described above.

    Below is my sample code SSCCE, containing my best try at getting this inner-loop logic to work. I know I'm failing with this logic, but I can't quite wrap my head around the right loop sequence:

    Java Code:
    public class arraycompare {
    
    	public static void main(String[] args) {
    
    //Setup array1		
    		Object[][] array1 = new Object[4][8];
    		array1[0][0] = "ABC"; 
    		array1[0][1] = "123"; 
    		array1[0][2] = "456"; 
    		array1[0][3] = "456";
    		array1[0][4] = "456";
    		
    		array1[1][0] = "DEF"; 
    		array1[1][1] = "789"; 
    		array1[1][2] = "111"; 
    		array1[1][3] = "456";
    		array1[1][4] = "456";
    		
    		array1[2][0] = "GHI"; 
    		array1[2][1] = "444"; 
    		array1[2][2] = "555"; 
    		array1[2][3] = "456";
    		array1[2][4] = "456";
    		
    		array1[3][0] = "XYZ"; 
    		array1[3][1] = "5AG"; 
    		array1[3][2] = "78R"; 
    		array1[3][3] = "456";
    		array1[3][4] = "456";
    		
    //Test print array1 in rectangular form
    		 int ROWS = array1.length;
    		 int COLS = array1[0].length;
    		 
    		 for (int L = 0; L < ROWS; L++) {
    			 for (int M = 0; M < COLS; M++) {
    				//out.write(" " + reportData[L][M]+"|");
    				System.out.print(" " + array1[L][M]);
    		 }
    			 //out.newLine();
    			 System.out.print("\n");
    		 }
    		 System.out.print("\n");		
    
    //Setup array2		 
    		Object[][] array2 = new Object[3][3];
    		array2[0][0] = "123"; 
    		array2[0][1] = "ABC"; 
    		array2[0][2] = "456"; 
    		
    		array2[1][0] = "000"; 
    		array2[1][1] = "XYZ"; 
    		array2[1][2] = "111"; 
    		
    		array2[2][0] = "GHI"; 
    		array2[2][1] = "444"; 
    		array2[2][2] = "555"; 
    		
    //Test print array2 in rectangular form
    		 int ROWS2 = array2.length;
    		 int COLS2 = array2[0].length;
    		 
    		 for (int L = 0; L < ROWS2; L++) {
    			 for (int M = 0; M < COLS2; M++) {
    				//out.write(" " + reportData[L][M]+"|");
    				System.out.print(" " + array2[L][M]);
    		 }
    			 //out.newLine();
    			 System.out.print("\n");
    		 }
    		 System.out.print("\n");			
    
    		 
    //Compare array1[0][i] to array2[1][d]
    //if match, insert array2[0-3][d] into array1[5-7][i]		
    		int array2index = 0;
    			for (int i = 0; i<array1.length; i++){
    				//System.out.println(array1[0][i]); 
    				for(int d = 0; d < array2.length; d++){
    					//System.out.println(array2[1][d]);
    					if(array1[0][i] == array2[1][d]){
    						for(int x = 5; x< array1[0].length; x++){
    							array1[i][x] = array2[d][array2index];
    							++array2index;
    						}					
    					}								
    				}
    			}
    		 
    //Test print array1 in rectangular form a second time to verify objective
    		 
    		 for (int L = 0; L < ROWS; L++) {
    			 for (int M = 0; M < COLS; M++) {
    				//out.write(" " + reportData[L][M]+"|");
    				System.out.print(" " + array1[L][M]);
    		 }
    			 //out.newLine();
    			 System.out.print("\n");
    		 }
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Compare 2 2D arrays and Conditionally Append Data

    Don't compare Strings for equality with the == operator; use the .equals( ... ) method instead. Read the API documentation for details.

    kind regards,

    Jos

    ps. I also think you've mixed up the row and column indexes of your arrays.
    Last edited by JosAH; 05-22-2012 at 11:08 AM.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Re: Compare 2 2D arrays and Conditionally Append Data

    Ok, so I've got the loop to find the matches and adjust array1 per above, but this seems like a weak way of doing it. Suggestions for improvements?

    Java Code:
    		 int matches = 0;
    			for (int i = 0; i<array1.length; i++){
    				//Object a1 = array1[i][0];
    				//System.out.println(a1);
    				for(int d = 0; d < array2.length; d++){
    					//Object a2 = array2[d][1];
    					//System.out.println(a2);				
    				if(array1[i][0].equals(array2[d][1]) ){
    					++matches;
    					//NEED LOOP
    					for(int z = 0; z < array2[0].length; z++){
    						array1[i][z+5] = array2[d][z];					
    					}
    					//array1[i][5] = array2[d][0];
    					//array1[i][6] = array2[d][1];
    					//array1[i][7] = array2[d][2];
    					//System.out.println("match: "+ matches + " match: " +array1[i][0] + " "+ array2[d][1] + "" +"\n");
    					int index = 0;
    					}
    				}
    			}
    Last edited by Redefine12; 05-22-2012 at 09:38 PM.

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2012, 01:59 PM
  2. How to append data to an already existing file?
    By siteregsam in forum New To Java
    Replies: 3
    Last Post: 05-03-2010, 08:06 PM
  3. compare arrays
    By innspiron in forum New To Java
    Replies: 4
    Last Post: 04-16-2010, 10:22 PM
  4. Compare two arrays for difference
    By aaronfsimons in forum New To Java
    Replies: 2
    Last Post: 05-11-2009, 03:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •