create class pair
class Pair{
String a
String b
....
///setters and getters
}
now let b will be List<Pair>
so instead calling b.add("{" + a.get(i)+ "," + a.get(j) + "}");
you can do simple b.add(new Pair(a.get(i),a.get(j));
then you don't need to play with splitting string and stuff like that, you can easly access your values by doing ie b.get(0).getA()
or b.get(0).get()
you can also override method to string in pair
public String toString() {
return "{" + a+ "," + b + "}";
}
so when you do System.out.println(a.get(0))
you will get exactly same output like before
***EDIT if you want to have a groups of more than 2 elements as you say in comment you can construct your class little bit different
class MyClass{
List<Integer> fields = new ArrayList<Integer>();
//two constructors
MyClass(int singleVal)
{
fields.add(singleVal);
}
MyClass(MyClass a, MyClass b)
{
fields.addAll(a.fields);
fields.addAll(b.fields);
}
//getters setters depends what you need
}
both of your list will be list of MyClass, when you populate list a, you create objects by using first constructor, when you want to add elements to your b list you can do b.add(new MyClass(a.(i),a.(j)))
but you can also do b.add(new MyClass(a.(i),b.(j)))
or b.add(new MyClass(b.(i),b.(j)))