2

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.

5
  • Please try to clarify your question and be more specific. Commented Apr 24, 2010 at 4:48
  • How would you do that in pencil and paper? What's the algorithm you have? Do you want us to do the program for you or are you having an specific Java syntax problem here? Commented Apr 24, 2010 at 5:09
  • He said he is a Java beginner (more like a beginner programmer), still, most of you guys introduced StringBuilder / Buffer to him :). I think the author of the question must a bit confused now. Commented Apr 24, 2010 at 7:42
  • @Andrei: I tried to address that part in my answer. I'll happily take any other suggestions you may have. Commented Apr 24, 2010 at 8:48
  • Why don't you accept the answer given by fastcodejava? Commented May 4, 2010 at 20:45

4 Answers 4

3

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!

Sign up to request clarification or add additional context in comments.

2 Comments

@M. Joanis: yeah, just noticed that; fixed now.
Using a method will simplify the answer : stackoverflow.com/questions/2703259/java-array-manipulation/…
3

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]);
    }
}

4 Comments

I'd rename end to length, and you should just provide build at this point.
@polygenelubricants : good suggestion, fixed. Method build should be easy to write.
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.
There's no need for ""+ as StringBuilder has append methods for any primitive, so sb.append(a[i]) works and is simpler.
1

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 Comment

@saching: but do you really want string as a1b1,a2a3b2b3,a4a5a6b4b5b6,.... so on up to n? ; )
0

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;
    }
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.