• Post Reply Bookmark Topic Watch Topic
  • New Topic

comparing specific elements of two arrays  RSS feed

 
David Santamaria
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

I’ve been working on this problem for a couple days already but I came to a point where I don’t really know what to do.
Basically I’ve got two arrays filled with int values (0, 1, and 2), which I get from Class B through their respective getMethods.

1 stands for black and 2 stands for something else.

The program counts how many times the value 1 (if chosen color is black) occurs in both arrays
and then compares the both counts. If it detects any difference the program does something else, otherwise it waits.




Here the class B.


this would be the Output:



The actual problem is that I will be getting one array at a time (meaning: the array int[] numbers from Class B updates itself and might change its values). I just declared and initialize both arrays for illustration purposes.
That would mean I need to hold the array’s values the first time I get them in a temporary variable until I get the values of the updated array. Then I could use them in the method –numberOfRelevantElements- and check if any changes have occurred.

What would be the best approach for doing this?

I though of inserting the different counts that I get into a queue and then comparing these values one after the other. But I’m not really sure if that would work.

I’d be grateful for any advice.

Thank you.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Santamaria wrote:
The actual problem is that I will be getting one array at a time (meaning: the array int[] numbers from Class B updates itself and might change its values). I just declared and initialize both arrays for illustration purposes.

What would be the best approach for doing this?


Question. Is the application single threaded or multithreaded?

If it is single threaded, then it is not an issue. It is not possible for the second array to change while you are fetching them.

If it is multithreaded, then how are you currently protecting the arrays? I am assuming that you have synchronization locks? ... So, this is a locking issue. You synchronize across only one method, but you need thread safety across two. You will need to make your locks more coarse, to allow the two method calls to be atomic.

Henry
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why are you using parallel arrays in the first place? That is an error‑prone technique. If you can find some way to get those value pairs into an object, that would be far better.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!