1

I have implemented a program that stores sets within a Linkedlist. When I print out the linked lists, I get, for example, [1,2,3,4,5].

What I want is to change the square brackets to braces e.g. '{' & '}'

I can't use string manipulations so I MUST override the toString method.

I am clueless on how to do this other than the fact I know i must create a subclass. It's the method itself that baffles me.

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)); } } } } 
4
  • A linked list is a pretty poor implementation of a set, since adding a new element, deleting an element, and checking for set inclusion are all linear-time operations. Maybe you should use a hash table or a tree instead (or use one of Java's built-in set implementations, which are based on hash tables and trees). Commented Jan 24, 2013 at 22:17
  • I googled 'java linkedlist source' and the 1st one points to source. But the 2nd one was Sun source and had clickable links to find parent classes. I found the source you could use here: AbstractCollection.java Commented Jan 24, 2013 at 22:18
  • What is the reason that you cannot use string manipulations? Commented Jan 24, 2013 at 22:27
  • I suggest you don't bother. Use the default. Otherwise you have to write code. Commented Jan 24, 2013 at 23:03

3 Answers 3

4

You could also create a helper method which takes List object as an attribute and returns String with concatenated contents of this list. That way inheritance is redundant.

Sign up to request clarification or add additional context in comments.

Comments

3

The output is completely dependent on the implementation of AbstractCollection.toString() - which LinkedList (and most other collections except maps) inherits. This implementation looks like this (slightly simplified):

public String toString() { Iterator<E> it = iterator(); if (! it.hasNext()) return "[]"; StringBuilder sb = new StringBuilder(); sb.append('['); for (;;) { E e = it.next(); sb.append(e); if (! it.hasNext()) return sb.append(']').toString(); sb.append(", "); } } 

Having this as a guideline it should be pretty straightforward for you to create your own implementation that does not include square brackets. You can either create your own collection that overrides toString() or have a separate utility method taking your LinkedList input as an argument (in that case obviously replace iterator() with input.iterator()).

Comments

1

Create a class that extends LinkedList and contains a method like this:

@Override public String toString() { } 

The method can access the list items like this:

Iterator<Integer> it = this.iterator(); while(it.hasNext()) { Integer nextItem = it.next(); } 

4 Comments

Dont tell me.. .. I didnt give all the code but should be enough info here to get going
It seems to me that extending LinkedList is a bit of overkill. Don't you think so?
he said he "must" Override toString
ye thats what i have, my problem is i dont know what to put inside the toString method. im not asking for the answer only advice

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.