我有一个问题,使用Arrays.sort()排序字符串数组

时间:2014-12-09 21:21:01

标签: java arrays sorting

所以我试图按字母顺序组织字符串数组并将它们插入到二叉树中。  我首先接受了一个文本文件,并使用扫描仪将其读入字符串。 然后我删除所有标点符号并将所有字母小写。 最后,我将我的字符串转换为单词数组,并使用Arrays.sort()对它们进行排序。 但是当我运行程序以查看是否可以打印我的列表时,我得到的唯一输出是:

[Ljava.lang.String; @ 4965391b

我不确定这意味着什么或为什么我将其作为输出请帮助。

我的代码如下。

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Arrays;

import java.util.Scanner;


public class Tester {

public static void main(String[] args) throws FileNotFoundException {

    Tester run = new Tester();
    run.it();

}

public void it() throws FileNotFoundException { 

    BTree theTree = new BTree();

    String str = this.readInFile();

    str = stripPunctuation(str);

    String myWords [] = breakIntoWords(str);

    //theTree.print();

}

public String readInFile() throws FileNotFoundException {


    String myFile = "";
    int numWords = 0;

    Scanner myScan = new Scanner(new File("Dracula.txt"));

    while(myScan.hasNext() == true) {

        myFile += myScan.nextLine() + " ";

    }

    return myFile;

}

public String stripPunctuation(String myFile) {

    myFile.replace('.', ' ');
    myFile.replace(',', ' ');
    myFile.replace('!', ' ');
    myFile.replace('?', ' ');
    myFile.replace('(', ' ');
    myFile.replace(')', ' ');
    myFile.replace('"', ' ');
    myFile.replace('[', ' ');
    myFile.replace(']', ' ');
    myFile.toLowerCase();
    return myFile;

}

public String [] breakIntoWords(String myFile) {

    BTree thisTree = new BTree();

    String[] words = myFile.split("\\s+");

    Arrays.sort(words);

    System.out.print(words);

    return words;

}

}

public class BTNode {

private BTNode rightChild;
private BTNode leftChild;
private String myWord;
private int numWords;
private int numInstance;
private boolean uniqueWord;
private boolean isRoot;
private boolean isDeepest;

public BTNode(String myWord, int numWords){

    this.numInstance = 1;
    this.myWord = myWord;
    this.numWords = numWords;
    this.rightChild = null;
    this.leftChild = null;

}

public String getMyWord() {
    return myWord;
}

public void setMyWord(String myWord) {
    this.myWord = myWord;
}

public BTNode getRightChild() {
    return rightChild;
}

public void setRightChild(BTNode rightChild) {
    this.rightChild = rightChild;
}

public BTNode getLeftChild() {
    return leftChild;
}

public void setLeftChild(BTNode leftChild) {
    this.leftChild = leftChild;
}

public int getnumWords() {
    return numWords;
}

public void setnumWords(int numWords) {
    this.numWords = numWords;
}

public boolean isUniqueWord() {
    return uniqueWord;
}

public void setUniqueWord(boolean uniqueWord) {
    this.uniqueWord = uniqueWord;
}

public boolean isRoot() {
    return isRoot;
}

public void setRoot(boolean isRoot) {
    this.isRoot = isRoot;
}

public boolean isDeepest() {
    return isDeepest;
}

public void setDeepest(boolean isDeepest) {
    this.isDeepest = isDeepest;
}

public int getNumInstance() {
    return numInstance;
}

public void setNumInstance(int numInstance) {
    this.numInstance = numInstance;
}

}

public class BTree {

private BTNode root;
private int nodeCount;


public boolean add(String word, int numWords){

    BTNode myNode = new BTNode(word, numWords);

    if(root == null){

        root = myNode;
        nodeCount++;
        return true;

    }

    if(findNode(word)){

        int tmp = myNode.getNumInstance();
        tmp++;
        myNode.setNumInstance(tmp);
        return false;

    }

    BTNode temp = root;

    while(temp != null){

        if(word.compareTo(temp.getMyWord()) < 0) {

            if(temp.getRightChild() == null){

                temp.setLeftChild(myNode);
                nodeCount++;
                return true;

            } else {

                temp = temp.getRightChild();

            }

        } else {

                if(temp.getLeftChild() == null){

                    temp.setLeftChild(myNode);
                    nodeCount++;
                    return true;

                } else {

                    temp = temp.getLeftChild();

                }

        }

    }

    return false;

}

public boolean findNode(String word) {
    return mySearch(root, word);
}

private boolean mySearch(BTNode root, String word) {
    if (root == null) {
        return false;
    }

    if ((root.getMyWord().compareTo(word) < 0)) {
        return true;
    } else {
        if (word.compareTo(root.getMyWord()) > 0) {
            return mySearch(root.getLeftChild(), word);
        } else {
            return mySearch(root.getRightChild(), word);
        }
    }
}

public void print() {
    printTree(root);
}

private void printTree(BTNode root) {
    if (root == null) {
        System.out.print(".");
        return;
    }

    printTree(root.getLeftChild());
    System.out.print(root.getMyWord());
    printTree(root.getRightChild());

}

public int wordCount() {

    return nodeCount;

}

}

1 个答案:

答案 0 :(得分:0)

您正在System.out.print中打印一个字符串数组,所以基本上 [Ljava.lang.String; @ 4965391b 是此数组引用的字符串表示形式。

如果更换零件

System.out.print(words);

for (String word : words) {
    System.out.println(" " + word);
}

你将得到数组中的元素。