我不熟悉编程和Java。我试图写我自己的数据类型/集合类,但我不太清楚如何去做。制作我自己的数据类型
我想要一个包含字符串的类和一个字符串数组列表。
通常我会去制作一个数据类型是这样的:
public class Collection {
private int id;
private String word;
private int howMany;
private char firstChar;
public Collection (int i, String w, int h, char f){
id = i;
word = w;
howMany = h;
firstChar = f;
}
//Getters and Setters
}
不知道我怎么能与一个ArrayList领域做到这一点。
我的目标背后是从.txt文件中读取字符串字段中的一类独特单词,并在arraylist字段中包含包含该单词的所有.txt文件名。
编辑:建筑汤姆·安德森的反应和我最初想的事:
public class Collection {
private int id;
private String word;
private int howMany;
private char firstChar;
private List<String> filenames;
public Collection (int i, String w, int h, char f, List<String> filenames){
id = i;
word = w;
howMany = h;
firstChar = f;
this.filenames = filenames;
}
}
我仍然不知道如何使用这个,因为我不是一个简单的字符串制作时添加到ArrayList参数我的Collection类的一个新实例。像这样的:
ArrayList<Collection> collection = new ArrayList<Collection>();
Collection newTest= new Collection("test","test");
collection.add(newTest);
如果你正试图实现一个搜索/索引,你可以利用lucene。您可以通过扩展集合或封装它来创建自己的集合。 – aishwarya
什么是您在最后一个片段中调用的'Collection'构造函数?你还没有显示一个构造函数,它之前需要两个字符串。 –