2

I have following two arrays

int a[]={1,2,3,4,5,6,7,8,9};

int b[]={4,2,3}

Output should be

b[0] has 4 so the first line of output should be  1,2,3,4 (from array a )
b[1] has 2 so the 2nd line of output should be 5,6  (from array a )
b[2] has 3 so the 3rd line of outout should be 7,8,9  (from array a )

the output should be like this

1,2,3,4
5,6
7,8,9

I am trying with this code but it's not working

for(i=0;i<b[j];i++)
     {
        if(flag==false){
        System.out.print(a[i]);
        flag=true;
        }
        else
            System.out.print(" "+a[i]);
     }
     j=j+1;
          System.out.print("\n");

Note : there can be any number of elements in array a and b

1
  • What is your initial value of j? How are you planning to loop back? Please include the j loop construct as well. (P.S: Check my answer, it seems to solve your question.) Commented May 23, 2015 at 9:35

3 Answers 3

1

We keep track of which index we've reached in a[], by saving the running total of the elements of b[], in the integer variable nextIndex.

Notice: A prerequisite for this code to work is, that the combined sum of the elements of b[] must not be larger than the number of elements in a[].

public static void main(String[] args) {

    int[] a = { 1,2,3,4,5,6,7,8,9 };
    int[] b = { 4,2,3 };

    int nextIndex = 0;

    for (int i = 0; i < b.length; i++) {

        for (int j = nextIndex; j < nextIndex + b[i]; j++) {

            if (j == (nextIndex + b[i] - 1)) {
                System.out.print(a[j]);
            }
            else {
                System.out.print(a[j] + ",");
            }

        }

        nextIndex += (b[i]);

        System.out.println();

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

Comments

1
int cur = 0;                                    //denotes index of array a
for (int i=0; i<b.length; i++) {                //looping on the elements of b

    int count = b[i], j;                       //count: number of elements to print is in b[i]
    for(j=cur; j<cur+count && j<a.length; j++) //looping from current index in array a to the next "count" elements which we need to print
       System.out.print(a[j]+",");             //print the elements for one line
    System.out.println();
    cur = j;                                  //updating the new index of array a from where to start printing  
}

3 Comments

can you add some explanation?
@AdrianCidAlmaguer added comments. Tell me if you need more help.
Yes, sometimes in a hurry I forget that we are not writing the code for ourselves, but for thousands of others. :)
1

One for loop is good enough to accomplish what you're wanting when you use Arrays.copyOfRange(int[] original, int from, int to) to extract "sub" arrays from your original array. Arrays.toString() prints the array for you.

public static void main(String[] args) throws Exception {
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int b[] = {4, 2, 3};

    int aIndex = 0;
    int bIndex = 0;
    for (int i = 0; i < b.length; i++) {
        bIndex += b[i]; // Keeps track of the ending index of the copying
        System.out.println(Arrays.toString(Arrays.copyOfRange(a, aIndex, bIndex)));
        aIndex += b[i]; // Keeps track of the starting index of the copying
    }
}

Results:

[1, 2, 3, 4]
[5, 6]
[7, 8, 9]

Comments

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.