Java implements Cartesian product algorithm example of unknown dimension set based on recursion and loop
This paper illustrates how Java implements Cartesian product of unknown dimension set based on recursion and loop. Share with you for your reference, as follows:
What is Cartesian product?
In mathematics, the Cartesian product of two sets X and y, also known as the direct product, is expressed as X × Y. The first object is a member of X and the second object is one of all possible ordered pairs of Y.
Suppose set a = {a, B}, set B = {0,1,2}, then the Cartesian product of two sets is {(a, 0), (a, 1), 2), (B, 2)}.
How to realize Cartesian product with program algorithm?
If the number of sets is known before programming, the Cartesian product can be obtained through multiple cycles of the program. But if you don't know the number of sets before programming, how to get the Cartesian product? For example, a set represents a list < list < string > > list; The number of lists before programming is unknown. The following code implements the Cartesian product of the unknown dimension set using recursive and cyclic methods:
The output is:
For more information about Java algorithms, readers who are interested can see the topics on this site: Java data structure and algorithm tutorial, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills