Java array manipulation

I am new to java. I want the logic of a small program.

I have two arrays

array = {a1,a2,a3,a4,a5,,,,,,,,,an}

      

and

array2 = {b1,b2,b3,b4,,,,,,,,,,,bn}

      

I need a string like a1b1, a2a3b2b3, a4a5a6b4b5b6, .... so on up to n

Please tell me what the logic will be.

+2


a source to share


4 answers


You need a nested for loop. You add span

-elements from arr1

and arr2

at a time, and this span

increases from 1. This code only works if N

is a triangular number ; otherwise, the last element will be "incomplete" and at the moment this code does not process it (and will throw it ArrayIndexOutOfBoundsException

).

String[] arr1 = { "a1", "a2", "a3", "a4", "a5", "a6" };
String[] arr2 = { "b1", "b2", "b3", "b4", "b5", "b6" };
int N = arr1.length;
// here we assume that N == arr2.length, and N is triangular

StringBuilder sb = new StringBuilder();
for (int start = 0, span = 1; ; span++) {
    for (int i = 0; i < span; i++) {
        sb.append(arr1[start + i]);
    }
    for (int i = 0; i < span; i++) {
        sb.append(arr2[start + i]);
    }
    start += span;
    if (start == N) break;
    sb.append(",");
}
System.out.println(sb);
// prints "a1b1,a2a3b2b3,a4a5a6b4b5b6"

      


Logics

To understand just the basic logic, perhaps we can start with something simpler:

int start = 0;
int N = 8;
int span = 1;
while (start < N) {
   System.out.println("span = " + span);
   for (int i = 0; i < span; i++) {
      System.out.println(start + i);
   }
   start += span;
   span++;
}

      



Prints:

span = 1
0
span = 2
1
2
span = 3
3
4
5
span = 4
6
7
8
9

      

You should understand how the nested loop structure works. Note again that even if N = 8

(which is not a triangular number), the loop actually prints 10 numbers (which is a triangular number). You can work with this snippet first, try changing it so that it only prints numbers N

, no matter if it is a triangular number, then adapt the same method to the original problem.

You can also use this snippet to print, say, "next!"

before every line span

except the first. It would be the same logic to include the comma in the original problem.

Good luck!

+3


a source


This will work.

int length = 1;
for (int start = 0; start < n; start += length, length++;) {
   build(builder, a1, start, length);
   build(builder, a2, start, length);
   if (start + length < n) {
       builder.append(",");
   }
}

      

The method build(StringBuilder builder, int[] a, int start, int end)

should be easy to write. This method will add a[start]

to the a[end]

check end < a.length

. At the end, remove the last comma.



EDIT : As requested from polygenic lubricants providing a complete answer. The cycle has also been changed above.

private void build(StringBuilder sb, int[] a, int start, int length) {
    for (int i = start; i < length && i < a.length - start; i++;) {
        sb.append(a[i]);
    }
}

      

+3


a source


Use a for loop and a StringBuilder instance.

String getHomework(int[] array, int[] array2){
    final int n = array.length; //assumes len array 1== len array2
    StringBuilder builder = new StringBuilder();

    for (int i=0;i<n-1;i++){
       builder.append(array[i]);
       builder.append(array2[i]);
       builder.append(",");
    }

    builder.append(array[n-1]);
    builder.append(array2[n-1]);

    return builder.toString();

      

}

+1


a source


Something like this should work ... not tested!

arr1 = {a11, a12, ..., a1n};
arr2 = {a21, a22, ..., a2n};

StringBuilder builder = new StringBuilder();

int index = 0;
for (int i = 1; true; i++) {
    for (int j = 0; j < i; j++) {
        builder.append("" + arr1[index + j]);
    }
    for (int j = 0; j < i; j++) {
        builder.append("" + arr2[index++]);
    }
    if (index < n - 1) {
        builder.append(",");
    } else {
        break;
    }
}

      

0


a source







All Articles