Java 我在使用array.sort()对字符串数组进行排序时遇到了一个问题

Java 我在使用array.sort()对字符串数组进行排序时遇到了一个问题,java,arrays,sorting,Java,Arrays,Sorting,所以我尝试按字母顺序组织和排列字符串,并将它们插入到二叉树中。 我首先接收了一个文本文件,并使用扫描仪将其读入字符串。 然后我去掉了所有的标点符号,把所有的字母都改成小写。 最后,我将字符串转换为单词的and数组,并使用array.sort()对它们进行排序。 但是,当我运行程序查看是否可以打印列表时,我得到的唯一输出是: [Ljava.lang.String;@4965391b 我不知道这意味着什么,也不知道为什么我要把它作为一个输出,请帮助 我的代码如下 import java.io.Fil

所以我尝试按字母顺序组织和排列字符串,并将它们插入到二叉树中。 我首先接收了一个文本文件,并使用扫描仪将其读入字符串。 然后我去掉了所有的标点符号,把所有的字母都改成小写。 最后,我将字符串转换为单词的and数组,并使用array.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;

}

}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.array;
导入java.util.Scanner;
公共类测试员{
公共静态void main(字符串[]args)引发FileNotFoundException{
测试仪运行=新测试仪();
run.it();
}
public void it()引发FileNotFoundException{
BTree theTree=新的BTree();
String str=this.readinfle();
str=标点符号(str);
字符串myWords[]=breakIntoWords(str);
//tree.print();
}
公共字符串readinfle()引发FileNotFoundException{
字符串myFile=“”;
int numWords=0;
Scanner myScan=new Scanner(新文件(“Dracula.txt”);
while(myScan.hasNext()==true){
myFile+=myScan.nextLine()+“”;
}
返回myFile;
}
公共字符串标点符号(字符串myFile){
myFile.replace('.','');
myFile.replace(“”、“”、“”);
myFile.replace(“!”,“);
myFile.replace(“”?“”,“”);
myFile.replace(“”(“”,“”);
myFile.replace(')','';
myFile.replace(“,”);
myFile.replace('[','');
myFile.replace(']','');
myFile.toLowerCase();
返回myFile;
}
公共字符串[]breakIntoWords(字符串myFile){
BTree thisttree=新的BTree();
String[]words=myFile.split(\\s+);
数组。排序(单词);
系统输出打印(文字);
返回单词;
}
}
公共类BTNode{
私有BTNode-righchild;
私有BTNode leftChild;
私有字符串myWord;
私人国际货币基金组织;
私家侦探;
私有布尔唯一字;
私有布尔isRoot;
私有布尔值最深;
公共BTNode(字符串myWord,int numWords){
这个数值等于1;
this.myWord=myWord;
this.numWords=numWords;
this.rightChild=null;
this.leftChild=null;
}
公共字符串getMyWord(){
返回myWord;
}
公共void setMyWord(字符串myWord){
this.myWord=myWord;
}
公共BTNode getRightChild(){
还权子;
}
公共无效setRightChild(BTNode rightChild){
this.rightChild=rightChild;
}
公共BTNode getLeftChild(){
返回leftChild;
}
公共void setLeftChild(BTNode leftChild){
this.leftChild=leftChild;
}
public int getnumWords(){
返回numWords;
}
公共void setnumWords(int numWords){
this.numWords=numWords;
}
公共布尔值isUniqueWord(){
返回唯一字;
}
public void setUniqueWord(布尔uniqueWord){
this.uniqueWord=uniqueWord;
}
公共布尔值isRoot(){
返回isRoot;
}
公共void setRoot(布尔值isRoot){
this.isRoot=isRoot;
}
公共布尔值IsDeep(){
返回最深;
}
public void setdeep(布尔值isdeep){
this.isdeep=isdeep;
}
public int getNuminInstance(){
返回亮度;
}
公共空隙沉降量(国际沉降量){
this.numInstance=numInstance;
}
}
公共类树{
私有节点根;
私有int节点计数;
公共布尔加法(字符串字,整数个字){
BTNode myNode=新的BTNode(单词,numWords);
if(root==null){
root=myNode;
nodeCount++;
返回true;
}
if(findNode(word)){
int tmp=myNode.getNuminInstance();
tmp++;
myNode.setNumInstance(tmp);
返回false;
}
BTNode temp=根节点;
while(temp!=null){
if(word.compareTo(temp.getMyWord())<0){
if(temp.getRightChild()==null){
临时setLeftChild(myNode);
nodeCount++;
返回true;
}否则{
temp=temp.getRightChild();
}
}否则{
if(temp.getLeftChild()==null){
临时setLeftChild(myNode);
nodeCount++;
返回true;
}否则{
temp=temp.getLeftChild();
}
}
}
返回false;
}
公共布尔findNode(字符串字){
返回mySearch(root,word);
}
私有布尔mySearch(BTNode根,字符串字){
if(root==null){
返回false;
}
if((root.getMyWord().compareTo(word)<0)){
返回true;
}否则{
if(word.compareTo(root.getMyWord())>0){
返回mySearch(root.getLeftChild(),word);
}否则{
返回mySearch(root.getRightChild(),word);
}
}
}
公开作废印刷品(){
打印树(根);
}
私有void打印树(BTNode root){
if(root==null){
系统输出打印(“.”);
返回;
}
printTree(root.getLeftChild());
System.out.print(root.getMyWord());
printTree(root.getRightChild());
}
公共int字数(){
返回nodeCount;
}
}

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

如果你更换零件

System.out.print(words);


您将获得数组中的元素。

您只是看到在数组上调用
toString()
的输出。这与排序无关。请尝试调用
Array.toString(words)
。字符串的可能副本也是不可变的。因此,您的strip-ponaction方法目前没有任何作用。
for (String word : words) {
    System.out.println(" " + word);
}