简体   繁体   中英

Printing elements from array in specific format

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

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  
}

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

    }
}

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM