how can I return array in given format using ArrayList in java?

59 views Asked by At

I want to return an array in the given format to print all the sub-sequences of an array.

format:

[ [1, 2, 3] [1, 2} [1, 3] [1] [2, 3] [2] [3] [] ]

The code I write :

static ArrayList<Integer> trv(ArrayList<Integer> num, int index, ArrayList<Integer> op, ArrayList<Integer> ans) {
    if (index >= num.size()) {
        // System.out.print(op);
        // System.out.println();
        ans.addAll(op);
        return ans;
    }
    // exclude
    int ele = num.get(index);
    op.add(ele);
    trv(num, index + 1, op, ans);
    op.remove(op.size() - 1);
    // include
    trv(num, index + 1, op, ans);

    return ans;
}
public static void main(String[] args) {
    ArrayList<Integer> n = new ArrayList<>();
    int index = 0;
    ArrayList<Integer> op = new ArrayList<>();
    ArrayList<Integer> ans = new ArrayList<>();

    n.add(1);
    n.add(2);
    n.add(3);
    System.out.println(trv(n, index, op, ans));

}

its output

[1, 2, 3, 1, 2, 1, 3, 1, 2, 3, 2, 3] 

Thanks for your time

1

There are 1 answers

1
Old Dog Programmer On

Trivia item: Java arrays are one dimensional. Java doesn't have 2D, 3D, 4D, ... arrays.

But, Java has Arrays of Arrays! Normally, an array of arrays is used as a 2D array. But, Guess what? Arrays of Arrays is the feature you are looking for! That's because arrays of arrays do not require the "inner" arrays to be all of the same length.

For example, if you want a table of distances between cities, you could save space by having rows of different lengths. Suppose you had 30 cities:

The zeroth row cold have 29 elements. The first row, 28, 2nd row 27, and so on. You sometimes see such "triangular" tables in a printed map or road atlas.

int [][] distanceTable = new int [29][];
for (int i = 0; i < 29; ++i) {
   distanceTable [i] = new int [29 - i] ;
}

... 

public int getMiles (int city1, int city2) {
  if (city1 == city2) return 0;
  if (city1 < city2)
     return  distanceTable [city2][city1];
  return distanceTable [city1][city2];
}

But, your sample code isn't using an array of primitives. It's using a Collection instead. Well, just as Java allows "nested arrays", it allows nested Collection objects.

 ArrayList<ArrayList<Integer>> nestArray = 
     new ArrayList<ArrayList<Integer>> ();
 ...
 // add a new, empty list to end
 nestArray.add (new ArrayList<Integer> ());
 ...
 nestArray.get(index).add(foo);  // autoboxing if foo is an int
 ... 
 int bar = nestArray.get(indexA).get(indexB);  // autounboxing
 ...
 nestArray.get(index).addAll(fooCollection);
 ...

You can use nested loops to iterate through all elements:

 for (List<Integer> line: nestArray) { 
    for (Integer bar: line) { 
 ... 

So far, I've avoided directly giving you code to fix your program. Instead, I've tried to present this material as a lesson or lecture. You should be able adapt the above to your needs.

By the way, for arrays of primitives, Java Arrays has a method that can display contents of nested arrays the way you want:

System.out.println (Arrays.deepToString(distanceTable));
...
ArrayList<ArrayList<Integer>> arr;
...
// output arr

int idx = 0;
int[][] arrOut = new int [arr.size][];
for (List<Integer> line : arr) {
   arrOut [idx++] = line.toArray(line); 
}

System.out.println (Arrays.deepToString(arrOut);

Triangular table example (That goes from short to long.)

Java Arrays API