gpt4 book ai didi

java - 检查元素是否已经在 J​​ava 数组中

转载 作者:行者123 更新时间:2023-11-30 07:26:55 24 4
gpt4 key购买 nike

我现在正在进行的项目涉及我从文本文件中读取单词并将它们加载到一个数组中(最终是一个二叉树,但这将在稍后完成)。我必须将单词和单词的频率(最初为 1)加载到数组中,因此我将这两个变量都打包到一个对象 WordNode 中。我能够将单词加载到数组中,但是当我尝试检查单词是否已经在数组中时,事情就分崩离析了。如果是,我必须将频率增加 1。但是,我的代码甚至不检查单词,只是简单地添加它(我假设它检查的是对变量的引用,而不是单词本身)。下面是我的 main 方法和 WordNode 类。

主要方法:

public class Driver {
/////////////// fields ///////////////
public static ArrayUnorderedList<WordNode> wordArray = new ArrayUnorderedList<WordNode>();
public static LinkedBinarySearchTree<WordNode> wordTree = new LinkedBinarySearchTree<WordNode>(); //tree to hold words

/////////////// methods ///////////////
public static void main(String[] args) throws Exception {
//ask for filename
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name of the file to read from: ");
Reader file = new FileReader(reader.readLine());

//read file
Scanner input = new Scanner(file);

while(input.hasNext()) {
//get words from file
String word = input.next();

//remove non-word characters and convert to lowercase
word = word.replaceAll("\\W", "");
word = word.toLowerCase();

//create node
WordNode newWord = new WordNode(word);

//if word is already in array
if(wordArray.contains(newWord)) {
System.out.println("Word is already in array");

//increment frequency by 1
int index = wordArray.find(newWord);
wordArray.list[index].setFrequency(wordArray.list[index].getFrequency() + 1);
System.out.println(newWord.getWord() + newWord.getFrequency());
} else {
System.out.println("Word is not yet in array");

//add word to tree
System.out.println(newWord.getWord());
wordArray.addToRear(newWord);
}
}

//insert into tree

//perform traversals on tree
}

WordNode 类:

public class WordNode {
protected String word;
protected WordNode left, right;
protected int frequency;

/**
* Creates a new node with the specified data.
* @param obj the element that will become a part of the new node
*/
WordNode(String obj) {
word = obj;
left = null;
right = null;
frequency = 1;
}

/**
* Gets the word.
* @return the word
*/
public String getWord() {
return word;
}

/**
* Sets the word.
* @param word the word to set
*/
public void setWord(String word) {
this.word = word;
}

/**
* Gets the left.
* @return the left
*/
public WordNode getLeft() {
return left;
}

/**
* Sets the left.
* @param left the left to set
*/
public void setLeft(WordNode left) {
this.left = left;
}

/**
* Gets the right.
* @return the right
*/
public WordNode getRight() {
return right;
}

/**
* Sets the right.
* @param right the right to set
*/
public void setRight(WordNode right) {
this.right = right;
}

/**
* Gets the frequency.
* @return the frequency
*/
public int getFrequency() {
return frequency;
}

/**
* Sets the frequency.
* @param frequency the frequency to set
*/
public void setFrequency(int frequency) {
this.frequency = frequency;
}
}

ArrayList类的一些方法:

/**
* Returns true if this list contains the specified element.
* @param target the element that the list is searched for
* @return true if the target is in the list, false if otherwise
*/
public boolean contains(T target) {
return (find(target) != NOT_FOUND);
}

/**
* Returns the array index of the specified element, or the
* constant NOT_FOUND if it is not found.
* @param target the element that the list will be searched for
* @return the integer index into the array containing the target element, or the NOT_FOUND constant
*/
public int find(T target) {
int scan = 0, result = NOT_FOUND;
boolean found = false;

if (!isEmpty()) {
while (!found && scan < rear) {
if (target.equals(list[scan])) {
found = true;
} else {
scan++;
}
}
}

if (found) {
result = scan;
}

return result;
}

最佳答案

您的代码不起作用的直接原因是 ArrayUnorderedList#contains()可能依赖于 equals()方法来确定条目是否在列表中。如果没有看到类的定义,就不可能知道。

因为您没有提供 equals() 的覆盖,它使用对象身份(默认来自 Object )所以每个 WordNode与众不同 WordNode .

如果你想使用ArrayUnorderedList那么你必须实现 WordNode#equals()以正确的行为。

但是,您应该考虑使用 Map<String,Integer> (或 Map<String,WordNode> )而不是存储频率。这样会快得多。

关于java - 检查元素是否已经在 J​​ava 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10113527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com