Skip to main content
added 540 characters in body
Source Link
tim
  • 25.3k
  • 3
  • 31
  • 76

Another advantage of this approach is, that you are separating the collection of the data from the printing of the data. It would be easy to change the print command with for example a command to print to file later on.

Because you are using recursion, you would need to pass it as a parameter:

private static void dumpList(String string, List l, StringBuilder sb) {
    int n = l.size();
    for (int i = 0; i < n; i++) {
        if (l.get(i) instanceof String) {
            sb.append("\n").append(string).append(i).append(" ").append(l.get(i)).append("\n");
            // or Version 2: 
            // sb.append(string + i + " " + l.get(i) + "\n");
        }
        if (l.get(i) instanceof List) {
            dumpList(string + i, (List) l.get(i), sb);
        }
    }
}

(The second version is a bit more readable, while the first version might be faster, depending on the Java implementation)

Because you are using recursion, you would need to pass it as a parameter:

private static void dumpList(String string, List l, StringBuilder sb) {
    int n = l.size();
    for (int i = 0; i < n; i++) {
        if (l.get(i) instanceof String) {
            sb.append("\n").append(string).append(i).append(" ").append(l.get(i));
        }
        if (l.get(i) instanceof List) {
            dumpList(string + i, (List) l.get(i), sb);
        }
    }
}

Another advantage of this approach is, that you are separating the collection of the data from the printing of the data. It would be easy to change the print command with for example a command to print to file later on.

Because you are using recursion, you would need to pass it as a parameter:

private static void dumpList(String string, List l, StringBuilder sb) {
    int n = l.size();
    for (int i = 0; i < n; i++) {
        if (l.get(i) instanceof String) {
            sb.append(string).append(i).append(" ").append(l.get(i)).append("\n");
            // or Version 2: 
            // sb.append(string + i + " " + l.get(i) + "\n");
        }
        if (l.get(i) instanceof List) {
            dumpList(string + i, (List) l.get(i), sb);
        }
    }
}

(The second version is a bit more readable, while the first version might be faster, depending on the Java implementation)

added 64 characters in body
Source Link
tim
  • 25.3k
  • 3
  • 31
  • 76

Should I use StringBuilder instead of + operator?

If performance is an issue, then yes, use StringBuilder (see here).

Because you are using recursion, you would need to pass it as a parameter:

private static void dumpList(String string, List l, StringBuilder sb) {
    int n = l.size();
    for (int i = 0; i < n; i++) {
        if (l.get(i) instanceof String) {
            sb.append("\n").append(string).append(i).append(" ").append(l.get(i));
        }
        if (l.get(i) instanceof List) {
            dumpList(string + i, (List) l.get(i), sb);
        }
    }
}

And then output the result in main:

public static void main(String[] args) {
    String s = "Foo";
    StringBuilder sb = new StringBuilder(100);
    [...]
    dumpList("Foo", l, sb);
    System.out.println(sb.toString());
}

Should I print it differently?

That is really your decision. I think that the way you are doing it is a bit confusing, I would at least add a space in the recursive call between string and i (so Foo12 becomes Foo1 2). Personally, I would prefer the output to look something like this: ["a string" (0), ["a" (1), "b" (1), "c"]"c" (1)], "spam" (0), "eggs"]"eggs" (0)] (where the numbers are the depth level), but it really depends on what you want to do with the code.

Also, right now, you are not printing the corresponding depth of the item (you are printing each position inside the depth-level). To do this, you would need another argument which you can increase every time you call dumpList:

private static void dumpList(String string, List l, StringBuilder sb, int depth) {
    [...]
            dumpList(string + i, (List) l.get(i), sb, depth + 1);
    [...]
}

And by the way, you are never using list b.

Should I use StringBuilder instead of + operator?

If performance is an issue, then yes, use StringBuilder (see here).

Because you are using recursion, you would need to pass it as a parameter:

private static void dumpList(String string, List l, StringBuilder sb) {
    int n = l.size();
    for (int i = 0; i < n; i++) {
        if (l.get(i) instanceof String) {
            sb.append("\n").append(string).append(i).append(" ").append(l.get(i));
        }
        if (l.get(i) instanceof List) {
            dumpList(string + i, (List) l.get(i), sb);
        }
    }
}

And then output the result in main:

public static void main(String[] args) {
    String s = "Foo";
    StringBuilder sb = new StringBuilder(100);
    [...]
    dumpList("Foo", l, sb);
    System.out.println(sb.toString());
}

Should I print it differently?

That is really your decision. I think that the way you are doing it is a bit confusing, I would at least add a space in the recursive call between string and i (so Foo12 becomes Foo1 2). Personally, I would prefer the output to look something like this: ["a string", ["a", "b", "c"], "spam", "eggs"], but it really depends on what you want to do with the code.

Also, right now, you are not printing the corresponding depth of the item (you are printing each position inside the depth-level). To do this, you would need another argument which you can increase every time you call dumpList:

private static void dumpList(String string, List l, StringBuilder sb, int depth) {
    [...]
            dumpList(string + i, (List) l.get(i), sb, depth + 1);
    [...]
}

And by the way, you are never using list b.

Should I use StringBuilder instead of + operator?

If performance is an issue, then yes, use StringBuilder (see here).

Because you are using recursion, you would need to pass it as a parameter:

private static void dumpList(String string, List l, StringBuilder sb) {
    int n = l.size();
    for (int i = 0; i < n; i++) {
        if (l.get(i) instanceof String) {
            sb.append("\n").append(string).append(i).append(" ").append(l.get(i));
        }
        if (l.get(i) instanceof List) {
            dumpList(string + i, (List) l.get(i), sb);
        }
    }
}

And then output the result in main:

public static void main(String[] args) {
    String s = "Foo";
    StringBuilder sb = new StringBuilder(100);
    [...]
    dumpList("Foo", l, sb);
    System.out.println(sb.toString());
}

Should I print it differently?

That is really your decision. I think that the way you are doing it is a bit confusing, I would at least add a space in the recursive call between string and i (so Foo12 becomes Foo1 2). Personally, I would prefer the output to look something like this: ["a string" (0), ["a" (1), "b" (1), "c" (1)], "spam" (0), "eggs" (0)] (where the numbers are the depth level), but it really depends on what you want to do with the code.

Also, right now, you are not printing the corresponding depth of the item (you are printing each position inside the depth-level). To do this, you would need another argument which you can increase every time you call dumpList:

private static void dumpList(String string, List l, StringBuilder sb, int depth) {
    [...]
            dumpList(string + i, (List) l.get(i), sb, depth + 1);
    [...]
}

And by the way, you are never using list b.

added 56 characters in body
Source Link
tim
  • 25.3k
  • 3
  • 31
  • 76

Should I use StringBuilder instead of + operator?

If performance is an issue, then yes, use StringBuilder (see here).

Because you are using recursion, you would need to pass it as a parameter:

private static void dumpList(String string, List l, StringBuilder sb) {
    int n = l.size();
    for (int i = 0; i < n; i++) {
        if (l.get(i) instanceof String) {
            sb.append("\n").append(string).append(i).append(" ").append(l.get(i));
        }
        if (l.get(i) instanceof List) {
            dumpList(string + i, (List) l.get(i), sb);
        }
    }
}

And then output the result in main:

public static void main(String[] args) {
    String s = "Foo";
    StringBuilder sb = new StringBuilder(100);
    [...]
    dumpList("Foo", l, sb);
    System.out.println(sb.toString());
}

Should I print it differently?

That is really your decision. I think that the way you are doing it is a bit confusing, I would at least add a space in the recursive call between string and i (so Foo12 becomes Foo1 2). Personally, I would prefer the output to look something like this: ["a string", ["a", "b", "c"], "spam", "eggs"], but it really depends on what you want to do with the code.

Also, right now, you are not printing the corresponding depth of the item (you are printing each position inside the depth-level). To do this, you would need another argument which you can increase every time you call dumpList:

private static void dumpList(String string, List l, StringBuilder sb, int depth) {
    [...]
            dumpList(string + i, (List) l.get(i), sb, depth + 1);
    [...]
}

And by the way, you are never using list b.

Should I use StringBuilder instead of + operator?

If performance is an issue, then yes, use StringBuilder (see here).

Because you are using recursion, you would need to pass it as a parameter:

private static void dumpList(String string, List l, StringBuilder sb) {
    int n = l.size();
    for (int i = 0; i < n; i++) {
        if (l.get(i) instanceof String) {
            sb.append("\n").append(string).append(i).append(" ").append(l.get(i));
        }
        if (l.get(i) instanceof List) {
            dumpList(string + i, (List) l.get(i), sb);
        }
    }
}

And then output the result in main:

public static void main(String[] args) {
    String s = "Foo";
    StringBuilder sb = new StringBuilder(100);
    [...]
    dumpList("Foo", l, sb);
    System.out.println(sb.toString());
}

Should I print it differently?

That is really your decision. I think that the way you are doing it is a bit confusing, I would at least add a space in the recursive call between string and i (so Foo12 becomes Foo1 2). Personally, I would prefer the output to look something like this: ["a string", ["a", "b", "c"], "spam", "eggs"], but it really depends on what you want to do with the code.

Also, right now, you are not printing the corresponding depth of the item. To do this, you would need another argument which you can increase every time you call dumpList:

private static void dumpList(String string, List l, StringBuilder sb, int depth) {
    [...]
            dumpList(string + i, (List) l.get(i), sb, depth + 1);
    [...]
}

And by the way, you are never using list b.

Should I use StringBuilder instead of + operator?

If performance is an issue, then yes, use StringBuilder (see here).

Because you are using recursion, you would need to pass it as a parameter:

private static void dumpList(String string, List l, StringBuilder sb) {
    int n = l.size();
    for (int i = 0; i < n; i++) {
        if (l.get(i) instanceof String) {
            sb.append("\n").append(string).append(i).append(" ").append(l.get(i));
        }
        if (l.get(i) instanceof List) {
            dumpList(string + i, (List) l.get(i), sb);
        }
    }
}

And then output the result in main:

public static void main(String[] args) {
    String s = "Foo";
    StringBuilder sb = new StringBuilder(100);
    [...]
    dumpList("Foo", l, sb);
    System.out.println(sb.toString());
}

Should I print it differently?

That is really your decision. I think that the way you are doing it is a bit confusing, I would at least add a space in the recursive call between string and i (so Foo12 becomes Foo1 2). Personally, I would prefer the output to look something like this: ["a string", ["a", "b", "c"], "spam", "eggs"], but it really depends on what you want to do with the code.

Also, right now, you are not printing the corresponding depth of the item (you are printing each position inside the depth-level). To do this, you would need another argument which you can increase every time you call dumpList:

private static void dumpList(String string, List l, StringBuilder sb, int depth) {
    [...]
            dumpList(string + i, (List) l.get(i), sb, depth + 1);
    [...]
}

And by the way, you are never using list b.

Source Link
tim
  • 25.3k
  • 3
  • 31
  • 76
Loading