Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparing Values of multi dimensional array

 
ShivPrakash Srivastava
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have two multi dimensional array.I want to check each value in the array.If the values are not equal,I want to add them in another array and if they are equal,I want to skip that iteration and continue till the last element in the array.when Iam trying,iam not able to iterate through the array element.Can any pls let me know the logic for this?

Code snippet will be really helpful.

Thanks and Regards
Shiv
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Reader,
Can you post what you have written till now? We can guide you from there on.
In general, to navigate a two-dimensional array, you need two nested loops, such as this:


For 'n' depth multi-dimensional array, you need 'n' nested loops.
Will it be better to use recursion?
 
ShivPrakash Srivastava
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thanks for the quick response.

For example:
String[]A = {"A","B","C","D"};
String[]B = {"A","B","D"};

Now I want to iterate through array A and compare with each element in array B and want to store in another array say C,the element which are not common(in this case its "C")

Thanks and Regards
Shiv
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ShivPrakash Srivastava:
...For example:
String[]A = {"A","B","C","D"};
String[]B = {"A","B","D"};...

These are single-dimension arrays. A "multi-dimensional" array in Java is an array of arrays. For example...
String[][] multi = { {"abc", "def"}, {"ghi", "jkl", "mno"} };
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your single-dimension array problem still requires a nested loop, as Shyam described above. However, instead of using the nested loop to iterate through a multi-dimensional array, you would do something like...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic