-4

I need function what print or return only list(not list of list) when if I called her i have

arr1 = [1, 2, [3, 4]];
arr1.flat(); 
// [1, 2, 3, 4]


arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
// [1, 2, 3, 4, 5, 6]

where 2 is number of nested

My Function

public static void included(ArrayList <Object> list) { 
    System.out.print("["); 
    for (int i = 0; i < list.size(); i++) { 
        System.out.print(list.get(i) + " "); 
    } 
    System.out.print("]"); 
}
Mohsen Shakibafar
  • 254
  • 1
  • 2
  • 15
Jackson750
  • 149
  • 11
  • 2
    What part of writing one do you need help with? – Scott Hunter Apr 22 '19 at 16:15
  • 2
    [How can I turn a List of Lists into a List in Java 8?](https://stackoverflow.com/questions/25147094/how-can-i-turn-a-list-of-lists-into-a-list-in-java-8), with both stream and non-stream answers. – rgettman Apr 22 '19 at 16:17

1 Answers1

0
    public static void included(ArrayList <Object> list) {

        StringBuilder s = new StringBuilder(list.toString()) ;
        System.out.println("BEFORE:");
        System.out.println(s);
        for (int i = 1; i < s.length(); ++i) {
            if ( s.charAt(i) == '[' ||s.charAt(i) == ']') {
                s.deleteCharAt(i);
            }
        }

        System.out.println("AFTER:");
        System.out.println(s);
}



    public static void included(ArrayList <Object> list,int size) {

        StringBuilder s = new StringBuilder(list.toString()) ;
        System.out.println("BEFORE:");
        System.out.println(s);
        int includeArrRight=0;
        int includeArrLeft=0;

            for (int i = 1; i <s.length(); i++) {
                if (s.charAt(i) == '[' && includeArrLeft < size) {
                    s.deleteCharAt(i);
                    includeArrLeft++;
                }

                if (s.charAt(i) == ']' && includeArrRight < size) {
                    s.deleteCharAt(i);
                    includeArrRight++;
                }
            }

        System.out.println("AFTER:");
        System.out.println(s);

        }
Jackson750
  • 149
  • 11