[英]I have an issue sorting an array of strings using Arrays.sort()
因此,我试图按字母顺序组织和排列字符串,并将其插入到二叉树中。 我首先输入一个文本文件,然后使用扫描仪将其读取为字符串。 然后,我删除了所有标点符号,并将所有字母都小写。 最后,我将字符串转换为单词数组,并使用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;
}
}
您正在System.out.print中打印一个字符串数组,因此基本上[Ljava.lang.String; @ 4965391b是对该数组的引用的字符串表示形式。
如果您更换零件
System.out.print(words);
与
for (String word : words) {
System.out.println(" " + word);
}
您将获得数组内的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.