tags:

views:

166

answers:

4

Hi,

I'm a beginner in java. I want the logic of the small program.

I have two arrays

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

and

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

I want string as a1b1,a2a3b2b3,a4a5a6b4b5b6,.... so on up to n

Please tell me what will be the logic.

A: 

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();

}

Tom
i want string as a1b1,a2a3b2b3,a4a5a6b4b5b6,.... so on up to n
sachin
@saching: but do you really want string as a1b1,a2a3b2b3,a4a5a6b4b5b6,.... so on up to n? ; )
Webinator
+3  A: 

You need a nested for-loop. You append span-elements from arr1 and arr2 at a time, with span increasing from 1. This code only works if N is a triangular number; otherwise the last element will be "incomplete" and as of now this code doesn't handle it (and would throw 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"

The logic

To understand just the underlying 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++;
}

This 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. Again, note that even though N = 8 (which is not a triangular number), the loop actually prints 10 numbers (which is a triangular number). You can work on this snippet first, try to modify it so that it will only print N numbers regardless of whether or not it's a triangular number, and then adapt the same technique to the original problem.

You can also work on this snippet to print, say, "next!" before each span line, except the first. That would be the same logic to include the comma in the original problem.

Good luck!

polygenelubricants
Asked for a2a3b2b3, not a2b2a3b3...
M. Joanis
@M. Joanis: yeah, just noticed that; fixed now.
polygenelubricants
Using a method will simplify the answer : http://stackoverflow.com/questions/2703259/java-array-manipulation/2703281#2703281
fastcodejava
+5  A: 

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 append a[start] to a[end] checking end < a.length. At the end remove the last comma.

EDIT : As per request from polygenelubricants providing complete answer. Above loop was changed a little as well.

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]);
    }
}
fastcodejava
i want string as a1b1,a2a3b2b3,a4a5a6b4b5b6,.... so on up to n
sachin
I'd rename `end` to `length`, and you should just provide `build` at this point.
polygenelubricants
@polygenelubricants : good suggestion, fixed. Method `build` should be easy to write.
fastcodejava
@fastcodejava: if it's easy then _just do it_, including the last comma removal part. This is a good answer and I'd upvote once it's complete.
polygenelubricants
There's no need for `""+` as StringBuilder has append methods for any primitive, so `sb.append(a[i])` works and is simpler.
Kevin Brock
A: 

Something like this should work... haven't tested though!

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;
    }
}
M. Joanis
Where i is the number of elements to print on the iteration for each array. index is the actual index of the element to print in the array. j are just there to loop i times.
M. Joanis