簡體   English   中英

從文件中讀取,然后創建私有 class 的對象

[英]read from file then create objects of private class

我在公共 class singlyLinkedList 中有私有 class 節點,我想從另一個公共 class 中移動列表(作為臨時),所以我如何創建臨時的 List.Node 或(方法) 我不能將 class 節點更改為公共節點,我應該保持私有。 編程思路是:在TextAnalyzer class中從文件中讀取數據,然后將其插入到SinglyLinkedList中,並統計單詞的頻率。 class 單鏈表

   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;

    }

class 文本分析器

  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;
    }
    }}

我們無法創建節點臨時,因為 class 節點是私有的,所以我不能為循環 go

您可能想要執行以下操作 1. 在 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. 使 SingleLinkedList 實現 Iterable
public class SinglyLinkedList<T> implements Iterable<T> {
  1. 調用 iterator() 時返回 1 中創建的迭代器實例
  @Override
  public Iterator<T> iterator() {
    return new MyIterator();
  }
  1. 用於文本分析器 class 中的每個循環

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM