一个java文件使用另一个java文件编译错误

时间:2014-06-16 11:28:19

标签: java

ubuntu版本:12.04 java version 1.7

FPTree.java内容:

package fptree;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


public class FPTree extends TreeNode {  


    private int minSuport;

    public int getMinSuport() {
        return minSuport;
    }

    public void setMinSuport(int minSuport) {
        this.minSuport = minSuport;
    }


    public List<List<String>> readTransRocords(String... filenames) {
        List<List<String>> transaction = null;
        if (filenames.length > 0) {
            transaction = new LinkedList<List<String>>();
            for (String filename : filenames) {
                try {
                    FileReader fr = new FileReader(filename);
                    BufferedReader br = new BufferedReader(fr);
                    try {
                        String line;
                        List<String> record;
                        while ((line = br.readLine()) != null) {
                            if(line.trim().length()>0){
                                String str[] = line.split(",");
                                record = new LinkedList<String>();
                                for (String w : str)
                                    record.add(w);
                                transaction.add(record);
                            }
                        }
                    } finally {
                        br.close();
                    }
                } catch (IOException ex) {
                    System.out.println("Read transaction records failed."
                            + ex.getMessage());
                    System.exit(1);
                }
            }
        }
        return transaction;
    }


    public void FPGrowth(List<List<String>> transRecords,
            List<String> postPattern) {

        ArrayList<TreeNode> HeaderTable = buildHeaderTable(transRecords);

        TreeNode treeRoot = buildFPTree(transRecords, HeaderTable);

        if (treeRoot.getChildren()==null || treeRoot.getChildren().size() == 0)
            return;

        if(postPattern!=null){
            for (TreeNode header : HeaderTable) {
                System.out.print(header.getCount() + "\t" + header.getName());
                for (String ele : postPattern)
                    System.out.print("\t" + ele);
                System.out.println();
            }
        }

        for (TreeNode header : HeaderTable) {

            List<String> newPostPattern = new LinkedList<String>();
            newPostPattern.add(header.getName());
            if (postPattern != null)
                newPostPattern.addAll(postPattern);

            List<List<String>> newTransRecords = new LinkedList<List<String>>();
            TreeNode backnode = header.getNextHomonym();
            while (backnode != null) {
                int counter = backnode.getCount();
                List<String> prenodes = new ArrayList<String>();
                TreeNode parent = backnode;

                while ((parent = parent.getParent()).getName() != null) {
                    prenodes.add(parent.getName());
                }
                while (counter-- > 0) {
                    newTransRecords.add(prenodes);
                }
                backnode = backnode.getNextHomonym();
            }

            FPGrowth(newTransRecords, newPostPattern);
        }
    }


    public ArrayList<TreeNode> buildHeaderTable(List<List<String>> transRecords) {
        ArrayList<TreeNode> F1 = null;
        if (transRecords.size() > 0) {
            F1 = new ArrayList<TreeNode>();
            Map<String, TreeNode> map = new HashMap<String, TreeNode>();

            for (List<String> record : transRecords) {
                for (String item : record) {
                    if (!map.keySet().contains(item)) {
                        TreeNode node = new TreeNode(item);
                        node.setCount(1);
                        map.put(item, node);
                    } else {
                        map.get(item).countIncrement(1);
                    }
                }
            }

            Set<String> names = map.keySet();
            for (String name : names) {
                TreeNode tnode = map.get(name);
                if (tnode.getCount() >= minSuport) {
                    F1.add(tnode);
                }
            }
            Collections.sort(F1);
            return F1;
        } else {
            return null;
        }
    }


    public TreeNode buildFPTree(List<List<String>> transRecords,
            ArrayList<TreeNode> F1) {
        TreeNode root = new TreeNode();
        for (List<String> transRecord : transRecords) {
            LinkedList<String> record = sortByF1(transRecord, F1);
            TreeNode subTreeRoot = root;
            TreeNode tmpRoot = null;
            if (root.getChildren() != null) {
                while (!record.isEmpty()
                        && (tmpRoot = subTreeRoot.findChild(record.peek())) != null) {
                    tmpRoot.countIncrement(1);
                    subTreeRoot = tmpRoot;
                    record.poll();
                }
            }
            addNodes(subTreeRoot, record, F1);
        }
        return root;
    }

    public LinkedList<String> sortByF1(List<String> transRecord,
            ArrayList<TreeNode> F1) {
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (String item : transRecord) {

            for (int i = 0; i < F1.size(); i++) {
                TreeNode tnode = F1.get(i);
                if (tnode.getName().equals(item)) {
                    map.put(item, i);
                }
            }
        }
        ArrayList<Entry<String, Integer>> al = new ArrayList<Entry<String, Integer>>(
                map.entrySet());
        Collections.sort(al, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Entry<String, Integer> arg0,
                    Entry<String, Integer> arg1) {

                return arg0.getValue() - arg1.getValue();
            }
        });
        LinkedList<String> rest = new LinkedList<String>();
        for (Entry<String, Integer> entry : al) {
            rest.add(entry.getKey());
        }
        return rest;
    }


    public void addNodes(TreeNode ancestor, LinkedList<String> record,
            ArrayList<TreeNode> F1) {
        if (record.size() > 0) {
            while (record.size() > 0) {
                String item = record.poll();
                TreeNode leafnode = new TreeNode(item);
                leafnode.setCount(1);
                leafnode.setParent(ancestor);
                ancestor.addChild(leafnode);

                for (TreeNode f1 : F1) {
                    if (f1.getName().equals(item)) {
                        while (f1.getNextHomonym() != null) {
                            f1 = f1.getNextHomonym();
                        }
                        f1.setNextHomonym(leafnode);
                        break;
                    }
                }

                addNodes(leafnode, record, F1);
            }
        }
    }

    public static void main(String[] args) {
        FPTree fptree = new FPTree();
        fptree.setMinSuport(3);
        List<List<String>> transRecords = fptree
                .readTransRocords("/home/steven/automata/test.txt");
        fptree.FPGrowth(transRecords, null);
    }
}

TreeNode.java文件内容

package fptree;

import java.util.TN_ArrayList;
import java.util.TN_List;

    public class TreeNode implements Comparable<TreeNode> {


        private String name; 
        private int count; 
        private TreeNode parent;
        private TN_List<TreeNode> children;
        private TreeNode nextHomonym;

        /*  
        public TreeNode() {

        }
        */
        public TreeNode(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }

        public TreeNode getParent() {
            return parent;
        }

        public void setParent(TreeNode parent) {
            this.parent = parent;
        }

        public TN_List<TreeNode> getChildren() {
            return children;
        }

        public void addChild(TreeNode child) {
            if (this.getChildren() == null) {
                TN_List<TreeNode> list = new TN_ArrayList<TreeNode>();
                list.add(child);
                this.setChildren(list);
            } else {
                this.getChildren().add(child);
            }
        }

        public TreeNode findChild(String name) {
            TN_List<TreeNode> children = this.getChildren();
            if (children != null) {
                for (TreeNode child : children) {
                    if (child.getName().equals(name)) {
                        return child;
                    }
                }
            }
            return null;
        }

        public void setChildren(TN_List<TreeNode> children) {
            this.children = children;
        }

        public void printChildrenName() {
            TN_List<TreeNode> children = this.getChildren();
            if (children != null) {
                for (TreeNode child : children) {
                    System.out.print(child.getName() + " ");
                }
            } else {
                System.out.print("null");
            }
        }

        public TreeNode getNextHomonym() {
            return nextHomonym;
        }

        public void setNextHomonym(TreeNode nextHomonym) {
            this.nextHomonym = nextHomonym;
        }

        public void countIncrement(int n) {
            this.count += n;
        }


        public int compareTo(TreeNode arg0) {

            int count0 = arg0.getCount();

            return count0 - this.count;
        }
    }

当我输入compile命令时:javac -d。 FPTree.java,它出现错误

FPTree.java:17: error: cannot find symbol
public class FPTree extends TreeNode {  
                            ^
  symbol: class TreeNode
FPTree.java:112: error: cannot find symbol
    public ArrayList<TreeNode> buildHeaderTable(List<List<String>> transRecords) {
                     ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:146: error: cannot find symbol
            ArrayList<TreeNode> F1) {
                      ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:145: error: cannot find symbol
    public TreeNode buildFPTree(List<List<String>> transRecords,
           ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:167: error: cannot find symbol
            ArrayList<TreeNode> F1) {
                      ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:196: error: cannot find symbol
    public void addNodes(TreeNode ancestor, LinkedList<String> record,
                         ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:197: error: cannot find symbol
            ArrayList<TreeNode> F1) {
                      ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:68: error: cannot find symbol
        ArrayList<TreeNode> HeaderTable = buildHeaderTable(transRecords);
                  ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:70: error: cannot find symbol
        TreeNode treeRoot = buildFPTree(transRecords, HeaderTable);
        ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:76: error: cannot find symbol
            for (TreeNode header : HeaderTable) {
                 ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:84: error: cannot find symbol
        for (TreeNode header : HeaderTable) {
             ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:92: error: cannot find symbol
            TreeNode backnode = header.getNextHomonym();
            ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:96: error: cannot find symbol
                TreeNode parent = backnode;
                ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:113: error: cannot find symbol
        ArrayList<TreeNode> F1 = null;
                  ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:115: error: cannot find symbol
            F1 = new ArrayList<TreeNode>();
                               ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:116: error: cannot find symbol
            Map<String, TreeNode> map = new HashMap<String, TreeNode>();
                        ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:116: error: cannot find symbol
            Map<String, TreeNode> map = new HashMap<String, TreeNode>();
                                                            ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:121: error: cannot find symbol
                        TreeNode node = new TreeNode(item);
                        ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:121: error: cannot find symbol
                        TreeNode node = new TreeNode(item);
                                            ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:132: error: cannot find symbol
                TreeNode tnode = map.get(name);
                ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:147: error: cannot find symbol
        TreeNode root = new TreeNode(); 
        ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:147: error: cannot find symbol
        TreeNode root = new TreeNode();
                            ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:150: error: cannot find symbol
            TreeNode subTreeRoot = root;
            ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:151: error: cannot find symbol
            TreeNode tmpRoot = null;
            ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:172: error: cannot find symbol
                TreeNode tnode = F1.get(i);
                ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:201: error: cannot find symbol
                TreeNode leafnode = new TreeNode(item);
                ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:201: error: cannot find symbol
                TreeNode leafnode = new TreeNode(item);
                                        ^
  symbol:   class TreeNode
  location: class FPTree
FPTree.java:206: error: cannot find symbol
                for (TreeNode f1 : F1) {
                     ^
  symbol:   class TreeNode
  location: class FPTree
28 errors

我的test.txt文件内容:

1,2,3,5
2,3,5
4,5,6
2,4,6
1,3,5,8

如何解决问题?请帮帮我

4 个答案:

答案 0 :(得分:0)

如果你真的想手动调用编译器,请确保构建一个正确的输出目录(不是.),将其与-classpath参数一起使用,并在右侧编译类订单(TreeNode之前的FPTree)。

或使用antmaven或IDE。

答案 1 :(得分:0)

尝试从一个文件夹上面编译代码:

javac parent/FPTree.java

或者你可以使用这个命令:

javac -classpath . *.java

答案 2 :(得分:0)

首先尝试编译TreeNode.java,因为FPTree.java取决于它。你不应该这样做(IIRC),但是到底是什么。此外,在编译FPTree时,使用-cp .选项告诉编译器它可以在哪里找到TreeNode的类文件。

当然,你也可以使用Maven,让你的生活更轻松>

答案 3 :(得分:0)

备注并提醒

  

当玩弄包装时,不要坐在这样的包装内!你必须在外面!

您的编译命令javac -d . FPTree.java包含一个关于您所在位置的微小提示。选项-d .可能意味着您正坐在名为fptree的目录中,该目录代表包。

拥有类似

的目录结构
<project_root>
      |
      |--- src/
      |     |--- fptree/
      |     |       |--- TreeNode.java
      |     |       |--- FPTree.java
      |
      |--- bin/

然后编译程序的最佳方法如下:转到项目根目录目录。从那里你用

调用java编译器
  

javac -d bin -sourcepath src TreeNode.java FPTree.java