4

Okay my aim is to read line by line from the standard input(a single String on each line),insert them into a tree , then sort the strings in ascending order and print them out.It prints

java.util.ArrayList$Itr@659e0bfd

QUESTION: What does this mean

return elementsList.iterator();

?How can I use it in my main?

public class OBTComparable<Type extends Comparable<Type>>
{
  ... 
  private Type data;
  ...
  public Iterator elementsAscending()
  {
    ArrayList<Type> elementsList = new ArrayList<Type>();
    addElementsAscending(elementsList);
    return elementsList.iterator();
  } // elementsAscending

  private void addElementsAscending(List elementsList)
  {
    if (!empty)
    { 
      left.addElementsAscending(elementsList);
      elementsList.add(data); 
      right.addElementsAscending(elementsList);
    } // if
  } // addElementsAscending 

Now the main:

public static void main(String[] args)
{
  OBTComparable<String> obt = new OBTComparable<String>();
  BufferedReader reader
    = new BufferedReader(new InputStreamReader(System.in));

  try
  {
    String line;
    while ((line = reader.readLine()) != null)
      obt.insert(line);
    obt.elementsAscending();
    System.out.println(obt.elementsAscending()); 
    ////// NEED HELP HERE /////


  } catch (IOException e) { System.out.println(e); };  
3
  • You need to implement you own BST? Commented Apr 29, 2015 at 17:47
  • Yes, because i am a beginner.If u have any suggestions please share with me.I want to learn how to implement it.Its probably a couple of lines of code but ... Commented Apr 29, 2015 at 17:51
  • If you make it a BST, then an in-order traversal of the tree will give you the sorted order. It's hard to tell, but it doesn't look like you have a valid BST here. Just maintain BST rules when inserting, and then have a method that does a in-order traversal of the tree. Commented Apr 29, 2015 at 18:14

1 Answer 1

2

Okay I fixed it.

String line;
  while ((line = reader.readLine()) != null)
      obt.insert(line);
    Iterator<String> t = obt.elementsAscending();
    while (t.hasNext())
    {
      String item = t.next();
      System.out.println(item);
    }

Feels good now :D .

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.