Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array difference quantity

 
Ricko Jonesy
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have some trouble with an easy question.
I want to compare two arrays to check their difference quantity.
Here is my Example:

Array Server: 1,2,3,4,5,6,7,8
Array Client: 1,2,3,4,8

The difference quantity should be 5,6 and 7, right?.


I tried to get it with a function which compares the arrays to equality, but this did'nt work:


for ( int i = 0; i < Array2.length; i++ ) {
for (int value: Array2) {
if (Array2[i] == Array1[i]) {
System.out.println("Platz"+i+" stimmt");
}
else
{
System.out.println("Platz"+i+" stimmt nicht");
}
}
}


May anybody help me? Thank you!
 
Ankit Nagpal
Ranch Hand
Posts: 47
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What is the exact nature of problem:

1) Do you want to display those numbers in Array1 which aren't at the same index as Array2?

or

2) Do you want to display those numbers in Array1 which do not exist in Array2?
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Collections this would be quite easy. Check Arrays.asList, Collection.addAll and Collection.removeAll.
 
Ricko Jonesy
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob: Could you please explain the procedure with the arraylists a bit closer?
So i have to add all of the Server-Array-Contents to one Arraylist and then subtract the contents of the Client-Array-Content via Collection.removeAll?

Thank you!
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be the trick yes. And because Collection.addAll and removeAll only accept other Collections, you need to convert the arrays to some Collection. And that's where my other suggestion can be used.

As said in the API of Set#removeAll:
If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets.

And that's just what you want.
 
Ricko Jonesy
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very mutch, it works perfect. more simple than expected.
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that if this is a homework question, your professor will most likely not approve that solution, since you're letting the API do the actual work.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic