我已经实现了一个将集合存储在Linkedlist
. 例如,当我打印出链接列表时,我会得到[1,2,3,4,5]
.
我想要的是将方括号更改为大括号,例如 '{' & '}'
我不能使用字符串操作,所以我必须重写该toString
方法。
除了我知道我必须创建一个子类之外,我对如何做到这一点一无所知。让我困惑的是方法本身。
public class outputSets
{
@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
public static void main(String args[])
{
LinkedList x0 = new LinkedList();
LinkedList x1 = new LinkedList();
LinkedList x2 = new LinkedList();
LinkedList x3 = new LinkedList();
LinkedList x4 = new LinkedList();
LinkedList x5 = new LinkedList();
LinkedList x6 = new LinkedList();
x0.add(new Integer(8));
for(int i=1; i<8; i++)
{
x1.add(new Integer(i));
}
x1.add((x0.getFirst()));
x2.addAll(x1);
x2.add(new Pair(1, x1));
x3.add(new Pair(x2,x1));
//x4.addAll(tree.union(x3, x2));
//x5.add(tree.difference(x4, x1));
//x6.add(tree.intersection(x4, x1));
/*
Iterator i0 = x0.listIterator();
Iterator i1 = x0.listIterator();
Iterator i2 = x0.listIterator();
Iterator i3 = x0.listIterator();
Iterator i4 = x0.listIterator();
Iterator i5 = x0.listIterator();
Iterator i6 = x0.listIterator();
*/
System.out.print(x0.getFirst());
System.out.println();
SetArray(x1);
System.out.println();
SetArray(x2);
System.out.println();
System.out.println();
}
@SuppressWarnings("rawtypes")
private static void SetArray(LinkedList x0)
{
for(int index=0; index < x0.size() ; index++)
{
if (index == 0)
{
System.out.print(x0.get(index));
}
else
{
System.out.print(", " +
"" + x0.get(index));
}
}
}
}