-1

I have an ArrayList and I'm trying to print it without the bracket ("[]") , I don't know where the problem lies , is it with how the ArrayList is initialized or is it the print method ?

The output that I'm getting :

S0 = (q0) = [0, 2] 
S1 = (q1) = [1, 0] 
S2 = (q2) = [2, 1] 

The output that I'm trying to get :

S0 = (q0) = {q0, q2} 
S1 = (q1) = {q1, q0} 
S2 = (q2) = {q2, q1} 

How can this be achieved ?

CODE :

import java.util.ArrayList;
import java.util.Scanner;
public class MyClass {
public static void printArray(String[][] data){
    System.out.print("    ");
    for (int i = 0; i < data[0].length; i++) {
        System.out.print("q" + i + " ");
    }
    System.out.println();
    for (int i=0;i< data.length;i++){
        System.out.print("q" + i + "  ");
        for (int j=0;j< data[0].length;j++){
            System.out.print(data[i][j] + "  ");
        }
        System.out.println();
    }
}
public static ArrayList findEpsilon(String[][] data){
    ArrayList<ArrayList<Integer> > aList = new ArrayList<>();
    ArrayList<Integer> a1;
    for (int i = 0; i < data.length ; i++) {
        a1 = new ArrayList<>();
        //aList = new ArrayList<>();
        a1.add(i);
        for (int j = 0; j < data[0].length; j++) {
            if (data[i][j].contains("eps")){
                a1.add(j);
            }
        }
        aList.add(a1);
    }
    return aList;
}
public static void printArrayList(ArrayList<ArrayList<Integer>> aList){
    for (int i = 0; i < aList.size(); i++) {
        for (int j = 0; j < aList.get(i).size(); j++) {
            System.out.println("S" + j + " = (q" + j + ") = " + aList.get(i).get(j) + " 
");
        }
       // System.out.println();
    }
}
public static void main(String args[]) {
    String[][] data = {
            { "a", "b", "eps"},
            { "eps", "a", "-" },
            { "a", "eps", "b"}};
    ArrayList<ArrayList<Integer> > aList = new ArrayList<ArrayList<Integer> >(3);
    printArray(data);
    System.out.println("\n");
    aList.add(findEpsilon(data));
    printArrayList(aList);
  
}
}
  • Do not use the raw type of `ArrayList`, see https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it. Always use the generic type. – Progman Apr 23 '22 at 20:17
  • If you print the contents of `aList` at the top of the `printArrayList` method, you'll see that the contents of your ArrayList are not what you think it is. – Roddy of the Frozen Peas Apr 23 '22 at 20:20

2 Answers2

1

The type of aList should be ArrayList<ArrayList<ArrayList<Integer>>>. The compiler doesn't complain about this because the findEpsilon method returns an ArrayList without type parameters. It's generally good practice to specify the generic type (i.e., findEpsilon should return ArrayList<ArrayList<ArrayList<Integer>>>). You will need to fix this in several places.

If you want to use curly brackets for the array, you can then use the following code:

for (int j = 0; j < aList.get(i).size(); j++) {
    String listAsString = aList.get(i).get(j).stream().map(Object::toString).collect(Collectors.joining(",", "{", "}"));
    System.out.println("S" + j + " = (q" + j + ") = " + listAsString + " ");
}

The stream iterates over each of the elements in the list and converts them to a string. The Collectors.joining collector then joins the individual strings by inserting a comma between them and placing curly brackets at the start and at the end.

Michel K
  • 411
  • 1
  • 7
0

You can define a Lambda to take a List<String> and convert it like you want.

  • put the list.toString version in a StringBuilder.
  • then change the first and last characters to { and } respectively
import java.util.function.Function;

Function<List<String>, String> fmt = lst-> {
    StringBuilder sb = new StringBuilder(lst.toString());
    sb.setCharAt(0,'{');
    sb.setCharAt(sb.length()-1,'}');
    return sb.toString();
};

Now apply a list to the lambda.

List<String> s = List.of("A","B","C");      
System.out.println(fmt.apply(s));

prints

{A, B, C}

WJS
  • 36,363
  • 4
  • 24
  • 39