Java 如何从不同的类文件中检索字符串,然后将该字符串添加到数组中,如果某些单词多次出现,则删除它们
我在这个问题上纠缠了几天,找不到任何答案。它慢慢地让我发疯。我需要创建两个方法,一个是将该单词添加到数组单词[]单词中,另一个是在该单词出现多次时将其删除。遗憾的是,每次我尝试使用.add或类似的东西时,它都不起作用。我很困惑 WordBagClient 这是我的WordBag.javaJava 如何从不同的类文件中检索字符串,然后将该字符串添加到数组中,如果某些单词多次出现,则删除它们,java,arrays,add,remove-if,Java,Arrays,Add,Remove If,我在这个问题上纠缠了几天,找不到任何答案。它慢慢地让我发疯。我需要创建两个方法,一个是将该单词添加到数组单词[]单词中,另一个是在该单词出现多次时将其删除。遗憾的是,每次我尝试使用.add或类似的东西时,它都不起作用。我很困惑 WordBagClient 这是我的WordBag.java 公共类WordBag{ public static final int DEFAULT_CAPACITY=8;//默认距离字数 //允许放在袋子里。 private int capacity;//此实例的私有数
公共类WordBag{
public static final int DEFAULT_CAPACITY=8;//默认距离字数
//允许放在袋子里。
private int capacity;//此实例的私有数组的容量
private Word[]words;//保存单词的数组
private int[]counts;//保存相应计数的数组
private int nextIndex;//指示下一个可用的元素位置
公共文字袋(){
this.words=新词[默认容量];
this.counts=新整数[默认容量];
}
公共字包(国际指定容量){
如果(容量>-1){
this.capacity=指定容量;
this.words=新词[容量];
this.counts=新整数[容量];
}否则{
this.capacity=默认容量;
this.words=新词[默认容量];
this.counts=新整数[默认容量];
}
nextIndex=0;
}
公共布尔值有(字){
布尔狗=假;
做{
if(words[nextIndex].equals(word)){
返回dog=true;
}否则{
nextIndex++;
}
}while(nextIndex)你能解释你的问题到底出在哪里吗?它发生在哪一行?请看一下你的逻辑。我怀疑索引没有得到正确维护(一个例子是remove()方法)。你为什么不使用counts数组来存储你的计数?以上所有这些似乎都是一个不完整的实现(还有一个类似于家庭作业的问题)。我建议使用Map来存储单词及其计数。我这个程序的确切问题是,我不明白应该使用什么来将单词存储到数组word[]单词中。每次使用.add或类似的方法时,我都会得到响应“无法旋转符号”或者类似于tho的东西。remove方法也是如此,这就是为什么我最终被迫尝试不使用数组的奇怪命令。你能解释一下你的问题到底出在哪里吗?它发生在哪一行?请看看你的逻辑。我怀疑索引没有正确维护(一个例子是remove()方法)。为什么不使用counts数组来存储计数?以上所有内容似乎都是一个不完整的实现(和一个类似家庭作业的问题)。我建议使用Map来存储单词及其计数。这个程序的确切问题是,我不明白应该使用什么来将单词存储到数组word[]单词中。每次使用.add或类似工具时,我都会得到响应“无法旋转符号”或者类似于tho的东西。remove方法也是如此,这就是为什么我最终被迫尝试不使用数组的奇怪命令。
public class WordBagClient {
public static void main(String[] args) throws IOException {
System.out.println("PMJ's WordBagClient ...");
String filename;
if(args.length > 0) { // Use run-time first argument value
filename = args[0];
}
else { // Prompt user to enter name of data file and store response
System.out.print("Enter name of input file:");
Scanner keyboard = new Scanner(System.in);
filename = keyboard.nextLine();
}
// Establish Scanner object that can read from the input data file.
Scanner input = new Scanner(new File(filename));
WordBag wordBag = new WordBag(256);
String s;
// Add each of the words until a blank line is encountered
do {
s = input.nextLine();
System.out.println(s);
if(s.length() > 0) {
addTask(s,wordBag);
}
} while (s.length() > 0);
printWordBag(wordBag);
// Remove an instance of each of the words until a second blank line is encountered
do {
s = input.nextLine();
System.out.println(s);
if(s.length() > 0) {
//removeTask(s,wordBag);
}
} while (s.length() > 0);
printWordBag(wordBag);
}
static void addTask(String s, WordBag wordBag) {
Scanner wordScanner = new Scanner(s);
while(wordScanner.hasNext()) {
String string = wordScanner.next();
String word = Word.wordOf(string);
wordBag.add(new Word(word));
}
}
static void removeTask(String s, WordBag wordBag) {
Scanner wordScanner = new Scanner(s);
while(wordScanner.hasNext()) {
String string = wordScanner.next();
String word = Word.wordOf(string);
Word object = new Word(word);
while(wordBag.multiplicityOf(object) > 0) {
wordBag.remove(object);
}
}
}
static void printWordBag(WordBag wordBag) {
System.out.println("The word bag now contains:");
System.out.println("--------------------------");
System.out.println(wordBag.toString());
System.out.println("--------------------------");
/*
wordBag.reset();
while(wordBag.hasNext()) {
System.out.print(wordBag.next());
if(wordBag.hasNext()) { System.out.print(","); }
}
System.out.println("\n"+"--------------------------");
*/
}
}
public class WordBag {
public static final int DEFAULT_CAPACITY = 8; //Default number of distince words
//allowed in a bag.
private int capacity; //The capacity of this instance's private arrays
private Word[] words; //The array to hold the words
private int[] counts; //The array to hold the corresponding counts
private int nextIndex; //Indicates the next available element position
public WordBag() {
this.words = new Word[DEFAULT_CAPACITY];
this.counts = new int[DEFAULT_CAPACITY];
}
public WordBag(int specifiedCapacity) {
if (capacity>-1){
this.capacity = specifiedCapacity;
this.words = new Word[capacity];
this.counts = new int[capacity];
}else{
this.capacity = DEFAULT_CAPACITY;
this.words = new Word[DEFAULT_CAPACITY];
this.counts = new int[DEFAULT_CAPACITY];
}
nextIndex = 0;
}
public boolean has(Word word) {
boolean dog = false;
do{
if(words[nextIndex].equals(word)){
return dog = true;
}else{
nextIndex++;
}
}while(nextIndex<=capacity && words[nextIndex]!=word);
return dog;
}
public int multiplicityOf(Word word) { //Stub!!!
int result=0;
do{
words[nextIndex].equals(word);
result = result+1;
}while(nextIndex<=capacity && words[nextIndex]!=word);
return result;
}
public void add(Word word) { //Stub!!!
do{
words[nextIndex]=(word);
}while(nextIndex<=capacity && words[nextIndex]!=word);
nextIndex++;
}
public void remove(Word word) { //Stub!!!
do{
if(multiplicityOf(word)>0){
word=null;
}else{
word=word;
}
}
while(nextIndex<=capacity && words[nextIndex]!=word);
}
private final String COMMA = ",";
public String toString() { //Stub!!!
String result = "";
result = Arrays.toString(words);
return result;
}
}
public class Word {
public static final String SYMBOLS = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";
private String word = "";
public Word(String string) {
String word = Word.wordOf(string);
if(Word.isWord(word)) {
this.word = word;
}
}
public String toString() {
return word;
}
public boolean equals(Word that) {
return this.compareTo(that) == 0;
}
public int compareTo(Word that) {
return this.toString().compareTo(that.toString());
}
public static String wordOf(String s) {
String result = "";
//Trim of leading and trailing whitespace
s = s.trim();
//Scan over leading symbols
int start;
for(start=0; (start<s.length()) && (SYMBOLS.indexOf(s.charAt(start))>=0); start++) {
}
//Scan over trailing symbols
int stop;
for(stop=s.length()-1; (stop > start) && (SYMBOLS.indexOf(s.charAt(stop))>=0); stop--) {
}
//Isolate what is left in the middle
if(start <= stop) {
result = s.substring(start,(stop+1));
}
//Return
return result;
}
public static boolean isWord(String s) {
s = s.trim();
return (s.length() > 0) && (s.equals(Word.wordOf(s)));
}
}