我不熟悉編程和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'構造函數?你還沒有顯示一個構造函數,它之前需要兩個字符串。 –