• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing arrays  RSS feed

 
Mark Robertson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please can anyone help.

I am trying to compare a 1 dimensional array with a two dimensional array to see if any of the values held are the same.

E.G - int [] array1 = {1, 2, 3, 4, 5};

int[] [] array2 = { {1, 2, 3, 5, 5},
{1, 3, 5, 6, 7},
{4, 5, 6, 7, 8} };


output = array2[1][y] "has 5 matching values"
array2 [2][y] "has 1 matching value"
array2[3][y] "has no matching values"

any suggestions much appreciated.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What code and/or ideas do you have so far?
 
Mark Robertson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi marc - to be honest I only have really vague ideas. Was thinking about searching the 2d array for the individual values held in the 1D array and wherever they are found replace them with something such as a string with value "empty". Then search again to determine how many times 'empty' is found on each row of the 2d array. As I said, very hazy at the moment. You know if I am on the right track or completely off the planet?. Thanks Mark
[ January 11, 2006: Message edited by: Mark Robertson ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before getting to the comparison issue, I think you need to figure out how you're going to iterate through these arrays. In other words, how can you set up loops to systematically look at each int value in array2:

array2[0][0]
array2[0][1]
...
array2[0][4]
array2[1][0]
array2[1][1]
...
array2[1][4]
array2[2][0]
array2[2][1]
...
array2[2][4]

Before you go too far down this road: The replacement concept could be made to work if you really wanted to do that, but that's making it much more complicated than it needs to be. (Note that these arrays hold ints, so you wouldn't be able to put Strings in them. Instead, you could use some special value like -1.) But why go through the array twice? And why perform replacements to count later? As soon as you know that you have a match, just increment the count.

But like I said, work out the iteration pattern first.
[ January 11, 2006: Message edited by: marc weber ]
 
Mark Robertson
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, you mean use the nessed 'for' loop to iterate accross the 2d array then search for the value in Array1[n]. Could I then create another array and increment its value by 1 if search a success. Hope I am not becoming too tiresome but Something like:



for (int loop = 0; loop < rows; loop++) {
for (int loop2 = 0; loop2 < columns; ball++) {
search = Arrays.binarySearch(Array2, Array1[0])

if(search >=0) {
output = Array1[loop][loop2] + Array3+1

where Array3 is an array with values set to 0
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, you mean use the nessed 'for' loop to iterate accross the 2d array then search for the value in Array1[n].

Yes, nested for loops are a good approach. But as for the "search"...

I gather from your original post that you are looking for matches based on position, which is why the third array has no matches even though it contains a '4' and a '5'. So it's probably not a binary search that you want. (That could be made to work, but it's getting overly complicated again.) The straightforward approach would be to simply compare values as you iterate through:

If(array1[0] == array2[0][0])...

Could I then create another array and increment its value by 1 if search a success.

Probably not another array. Just a int variable.

Again, focus on the iteration code first. As a way of verifying this, try writing nested for loops that print out each value in the 2-d array. Once you have this, it will be easy to add the comparison.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!