简体   繁体   中英

read from file then create objects of private class

I have private class node inside public class singlyLinkedList and I want to move through the List (as temp) from another public class, so how I can creat temporary Node or(method) to move through the List. and I can't change the class Node to public and I should keep to private. the idea of programming is: the read data from file in TextAnalyzer class, then insert it in SinglyLinkedList with counting the frequency of words. class singlyLinkedList

   public class SinglyLinkedList<T> {
   private static class Node<T> {
   public T data;
    Node<T> next;
    public Node(T data) {
        this.data = data;
        next = null;
    }
}
Node<T> head;
Node<T> tail;
int size;

public SinglyLinkedList() {
    head = null;
    tail = null;
    size = 0;
}

public void insert(T S) {
    Node<T> temp = new Node<T>(S);
    if (head == null) {
        head = tail = temp;
        size++;
        return;
    }

    temp.next = head;
    head = temp;
    size++;
    return;
}

public void display() {
    Node<T> tmp = head;
    while (tmp != null) {
        System.out.println(tmp.data.toString());
        tmp = tmp.next;

    }

The class TextAnalyzer

  SinglyLinkedList<WData> list = new SinglyLinkedList<WData>();
   private static class WData {

    String word;
    int freq;

    public WData(String w, int f) {
        word = w;
        freq = f;
    }

    // .. Add other methods here as needed
    @Override
    public String toString() {
        // if(list.)
        return "WData{" + "word=" + word + " , freq=" + freq + '}';
    }
}
  public Scanner sc;

public void processText(String filename) {

    try {
        sc = new Scanner(new File(filename));
        while (sc.hasNext()) {
            String line = sc.next();
            String[] st = line.split(" ");

            for (int i = 0; i < st.length; i++) {

                processWord(st[i]);
        }}
    list.display();
       } catch (FileNotFoundException ex) {
        System.out.println("error in loadstudends Scanner");
    }
}
 public void processWord(String word) {
         Node<WData> temp = list.head;

    while (temp != null) {
        if (temp.data.word.equalsIgnoreCase(word)) {
            break;
        }
        temp = temp.next;
    }
    if (temp == null || Dtemp.data.word.matches(".*\\d.*")) {

        list.insert(new WData(word, 1));

    } else {
        temp.data.freq += 1;
    }
    }}

we can't creat node temp because class node is private, so I couldn't go for the loop

You may want to do the following 1. Create your own Iterator implementation within SinglyLinkedList

public class MyIterator implements Iterator<T> {
    private Node<T> next = head;
    private Node<T> current = null;
    @Override
    public boolean hasNext() {
      return next != null;
    }
    @Override
    public T next() {
      if (hasNext()) {
        current = next;
        next = current.next;
        return current.data;
      }
      throw new NoSuchElementException();
    }

    @Override
    public void remove() {
      //TODO
    }
  }
  1. Make SinglyLinkedList to implement Iterable
public class SinglyLinkedList<T> implements Iterable<T> {
  1. return instance of iterator created in 1 when iterator() is invoked
  @Override
  public Iterator<T> iterator() {
    return new MyIterator();
  }
  1. Use for each loop in your Text Analyzer class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM