I have two array like this.
String[] arr1 = { "1", "2", "3" };
String[] arr2 = { "111", "222", "333", "444", "555", "666", "777", "888", "999" };
I want to merge these two array using combination of index value.
My input will be two integer value (2:3 ratio), like this
int firstArray = 2; //input value
int secondArray = 4; //input value
After merge all value will be stored in single list. Now i need output like this.
1
2
111
222
333
444
3
1
555
666
777
888
2
3
999
111
222
333
loop should run till which array length is big and retrive all values from both array.
if array length got changed then output ratio also should change
String[] arr1 = { "1", "2", "3", "4", "5", "6", "7", "8" };
String[] arr2 = { "111", "222", "333", "444", "555" };
int firstArray = 3; //input value
int secondArray = 2; //input value
output :
1
2
3
111
222
4
5
6
333
444
7
8
1
555
111
so condition is output should contain all values from both array and till maximum length of array and output should completed with second ratio(second input value - secondArray).
thanks in advance.
String[] arr = mergeArrays(arr1, arr2, 2, 3);
System.out.println("Ratio 2:3");
for (String str : arr) {
System.out.println(str);
}
private static String[] mergeArrays(String[] arr1, String[] arr2, int firstArray, int secondArray) {
final String[] ret = new String[arr1.length + arr2.length];
for (int j = 0, k = 0; j < arr1.length || k < arr2.length;) {
if (j < arr1.length) {
do {
ret[j + k] = arr1[j];
j++;
} while (j < arr1.length && (j % firstArray != 0 || k == arr2.length));
}
if (k < arr2.length) {
do {
ret[j + k] = arr2[k];
k++;
} while (k < arr2.length && (k % secondArray != 0 || j == arr1.length));
}
}
return ret;
}
I tried from this solution
but i cant able to bring correct out put
I used List instead of arrays, because I don't think you can really predict the length of the resulting array.
public static <T> List<T> merge(List<T> l1, List<T> l2, int r1, int r2) {
List<T> result = new ArrayList<T>();
int index1 = 0;
int index2 = 0;
while (index1 < l1.size() || index2 < l2.size()) {
for (int i = 0; i < r1; ++i)
result.add(l1.get((index1 + i) % l1.size()));
index1 += r1;
if (index2 < l2.size()) {
for (int i = 0; i < r2; ++i)
result.add(l2.get((index2 + i) % l2.size()));
index2 += r2;
}
}
return result;
}
Test code:
String[] arr1 = { "1", "2", "3" };
String[] arr2 = { "111", "222", "333", "444", "555", "666", "777",
"888", "999" };
System.out.println(merge(Arrays.asList(arr1), Arrays.asList(arr2), 2, 4));
Output:
[1, 2, 111, 222, 333, 444, 3, 1, 555, 666, 777, 888, 2, 3, 999, 111, 222, 333]
This should work:
public static void main(String args[]) throws IOException {
int[] array1 = {1, 2, 3, 4, 5, 6, 7, 8};
int[] array2 = {11, 22, 33};
int firstArray = 3;
int secondArray = 5;
for (int a1 = 0, a2 = 0; ;) {
for (int i = 0; i < firstArray; i++)
System.out.println(array1[a1++ % array1.length]);
if (a1 >= array1.length && a2 >= array2.length)
break;
for (int i = 0; i < secondArray; i++)
System.out.println(array2[a2++ % array2.length]);
if (a1 >= array1.length && a2 >= array2.length)
break;
}
}
If you want to generate a new array instaed of print result on video, simply declare an ArrayList and add elements to it using the add() method:
public static void main(String args[]) throws IOException {
int[] array1 = {1, 2, 3, 4, 5, 6, 7, 8};
int[] array2 = {11, 22, 33};
int firstArray = 3;
int secondArray = 5;
ArrayList<Integer> output = new ArrayList();
for (int a1 = 0, a2 = 0; ;) {
for (int i = 0; i < firstArray; i++)
output.add(array1[a1++ % array1.length]);
if (a1 >= array1.length && a2 >= array2.length)
break;
for (int i = 0; i < secondArray; i++)
output.add(array2[a2++ % array2.length]);
if (a1 >= array1.length && a2 >= array2.length)
break;
}
for (int i = 0; i < output.size(); i++)
System.out.println(output.get(i));
}
If you want to create a function:
public ArrayList<Integer> merge(int[] array1, int[] array2, int firstArray, int secondArray) {
ArrayList<Integer> output = new ArrayList();
for (int a1 = 0, a2 = 0; ;) {
for (int i = 0; i < firstArray; i++)
output.add(array1[a1++ % array1.length]);
if (a1 >= array1.length && a2 >= array2.length)
break;
for (int i = 0; i < secondArray; i++)
output.add(array2[a2++ % array2.length]);
if (a1 >= array1.length && a2 >= array2.length)
break;
}
return output;
}
If you don't want to return an ArrayList, simply convert it to an int[]
来源:https://stackoverflow.com/questions/27860900/dynamic-array-merge-in-java