Java 在不同类中使用Arraylist
因此,下面的代码会根据单词输入查找文档中特定的单词。计算单词在每个句子中出现的次数,然后将该次数存储在底部标签a(cone)和b(ctwo)的ArrayList中 我想在另一个类中使用ArrayList,但似乎找不到方法Java 在不同类中使用Arraylist,java,arraylist,Java,Arraylist,因此,下面的代码会根据单词输入查找文档中特定的单词。计算单词在每个句子中出现的次数,然后将该次数存储在底部标签a(cone)和b(ctwo)的ArrayList中 我想在另一个类中使用ArrayList,但似乎找不到方法 import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.ArrayList; import java.uti
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class exc {
public exc() {
}
public static void main(String[] args) throws Exception {
cone aa = new cone();
ctwo bb = new ctwo();
// after this I'm stuck
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
import java.util.List;
public class cone {
public void cone() throws Exception {
BufferedReader e = new BufferedReader(new FileReader("words to be read.txt"));
String o;
while((o = e.readLine()) != null){
String[] sentences = o.split("\\b[.!?]\\s+");
//System.out.println(o);
String [] h = sentences;
{
BufferedReader t = new BufferedReader(new FileReader("Text to be scan.txt"));
String g;
while((g = t.readLine()) != null){
String[] set=g.split(" ");
List<String> list = Arrays.asList(set);
// System.out.println(Arrays.toString(set));
//System.out.println(o);
int sentenceNumb=1;
for (String sentence: h) {
int counter=0;
String[] words = sentence.replace(".", "").split(" ");
for(String word: words) {
if (list.contains(word)) {
counter++;
}
}
List<Integer> A = Arrays.asList(counter++);
}
}
}
}
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
import java.util.List;
public class ctwo {
public void ctwo() throws Exception {
BufferedReader e = new BufferedReader(new FileReader("words to be read.txt"));
String o;
while((o = e.readLine()) != null){
String[] sentences = o.split("\\b[.!?]\\s+");
//System.out.println(o);
String [] h = sentences;
{
BufferedReader t = new BufferedReader(new FileReader("Text to be scan.txt"));
String g;
while((g = t.readLine()) != null){
String[] set=g.split(" ");
List<String> list = Arrays.asList(set);
// System.out.println(Arrays.toString(set));
//System.out.println(o);
int sentenceNumb=1;
for (String sentence: h) {
int counter=0;
String[] words = sentence.replace(".", "").split(" ");
for(String word: words) {
if (list.contains(word)) {
counter++;
}
}
List<Integer> B= Arrays.asList(counter++);
}
}
}
}
}
}
最好的方法是:在main中有两个ArrayList,将它们作为函数参数从任何需要它们的类传递给函数
不太好的方法:将ArrayList作为包保护的静态类变量存储在cone和ctwo类中。您可以通过cone.A和ctwo.B访问它们
你的程序看起来很奇怪。
我建议阅读单词并将不同的单词添加到hashmap中,关键字作为单词,值作为计数。在两个类的构造函数中传递相同的数组列表。使两个方法都返回ArrayList?如何?我是java新手你怎么做?我怎么做?