Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.stackOverflow.questions;
- public class PrintArrays
- {
- static int X = 5; // for example
- static int N = 7; // for example
- public static void main(String[] args)
- {
- //get X and N from the user.
- int arr[] = new int[N];
- doYourThing(arr, 0, 0);
- }
- public static void doYourThing(int arr[], int num, int mode) //mode = is he need to count up or stop when he get to X
- {
- if(num == N)
- {
- return;
- }
- int alreadyHas = (num != 0 && mode != 1)?(1):(0);
- for(int i = alreadyHas; i < X; i++)
- {
- arr[num] = i;
- printArr(arr);
- if(num > 0)
- {
- doYourThing(arr, num-1, 1);
- }
- }
- arr[num] = 0;
- if(mode == 0)
- {
- doYourThing(arr, num+1, 0);
- }
- }
- public static void printArr(int arr[])
- {
- String str;
- str = "[";
- for(int i = arr.length-1; i >= 0; i--)
- {
- str += arr[i] + ",";
- }
- str = str.substring(0, str.length()-1);
- str += "]";
- System.out.println(str);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement