int [10] array_int=new int [10];
array_int[0]=0;
array_int[1]=1;
array_int[2]=2;
array_int[3]=3;
array_int[4]=4;
array_int[5]=5;

question is:
how to print in circular manner starting from the middle(index is 2) that is
output :

2 3 4 5 0 1 .

Dani AI

Generated

wanted to print the array starting at an arbitrary index and wrap back to the front. 's wrap‑around idea is exactly the right direction; a slightly more compact and idiomatic approach in Java is to use modulo arithmetic to compute each output index. This also makes it trivial to handle negative or out‑of‑range start values and to print any number of elements (not necessarily the whole array).

static void printCircular(int[] a, int start) {
    if (a == null || a.length == 0) return;
    int n = a.length;
    start = ((start % n) + n) % n;  // normalize start into 0..n-1
    for (int i = 0; i < n; i++) {
        System.out.print(a[(start + i) % n]);
        if (i < n - 1) System.out.print(" ");
    }
    System.out.println();
}

Notes and edge cases:

  • Normalize the start index as shown so negative values or starts >= length work predictably.
  • If you only want to print count elements (count <= n), replace i < n with i < count.
  • If the array has a logical size smaller than a.length (e.g., capacity 10 but only 6 valid entries), use that logical size in place of n when computing indices.
  • Complexity is O(n) time and O(1) extra space. If you need to rotate a List in place, Collections.rotate or an ArrayDeque can be more appropriate.

For the example in the first post (start = 2 on [0,1,2,3,4,5]) this prints: 2 3 4 5 0 1.

Recommended Answers

All 6 Replies

Loop with an index variable that starts from 2, but then when you increment it test if it is >= (length of the array), in which case set it back to 0.

ok
i tried wat u mentioned

import java.util.LinkedList;
import java.util.ListIterator;
import java.io.*;
public class llist {


    public static void main(String[] args) {
        int[] arr ;
        arr=new int[10];
        arr[0]=1;
        arr[1]=2;
        arr[2]=3;
        arr[3]=4;
        arr[4]=5;
        int i;
        for( i=2;i<=5;i++){

        System.out.println(arr[i]);
        }
        if(i==6){
            i=0;
            System.out.println(arr[i]);
            }
        if(i==0){
            i=1;
            System.out.println(arr[i]);
        }


    }

}

is this correct?
but wat if i want to print array from the index 3 or 4 the if condition wat i have mentioned will b executed no matter wat.

I can't give you a complete homework solution, but here's some pseudo-code that shows how to approach it

int offset = 2;  // this is the first element to print, 0 - (length of array -1
for i= 0 to (length of array -1)    // this controls how many numbers to print
   print array[offset]
   offset = offset + 1
   if (offset >= (array length)) offset = 0
}

Thanks got it .

Museful: nobody still followed this thread, it's ancient, dead and marked as 'Solved'. no reason to revive it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.