Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Comparing Values of multi dimensional array

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic