Answer the question
In order to leave comments, you need to log in
Multiplying arrays of values in Java?
Good afternoon!
There is some need to obtain a multiplied array of strings.
Type:
array1 = april, may, june
array2 = 1992,1993,1994
array3 = Moscow, St. Petersburg
It is necessary to get all variants of the type:
Moscow, April, 1992
Moscow, April, 1993
Moscow, April, 1994
Moscow, May, 1992
... ..
SPb, June, 1994
Compiled the following code in Java, with simple loops:
for (var i = 0; i < array1.length; i++) {
if (array1[i] != "") {
for (var t= 0; t< array2.length; t++) {
if (array2[t] != "") {
for (var q=0; q< array3.length; q++) {
if (array3[q] != "") {
for (var z=0; z< Array4.length; z++) {
if (array4List[z] != "") {
listResult = listResult + array1[i].trim () + " " + array2[t] + " "+array3[q]+"\r\n";
}
}
}
}}}}}}
I would like to know if there are any other ways besides using such a number of loops? (As an upcoming task, I will need to multiply about 8 such arrays)
There is a solution in other languages, I'll try to figure it out.
Answer the question
In order to leave comments, you need to log in
multiply, java...
You need to make combinations in Javascript.
Google this way
codereview.stackexchange.com/questions/7001/genera...
I think you are confusing array multiplication with string concatenation. And also confuse Java and JavaScript - they are two different languages with significant differences.
And yes, arrays will have to be bypassed with at least some iterator.
Like this: https://jsfiddle.net/7p7xuLov/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question