[英]Making my own data type
我是編程和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;
}
}
我仍然不確定如何使用它,因為我不能簡單地在創建我的Collection類的新實例時向ArrayList參數添加一個字符串。 比如這樣:
ArrayList<Collection> collection = new ArrayList<Collection>();
Collection newTest= new Collection("test","test");
collection.add(newTest);
在我繼續之前, Collection
對於一個類來說不是一個好名字,因為在java.util
包中已經存在一個非常流行的該類名。 我會把你的班級稱之為事件。
從表面上看你的問題,它將是:
public class Occurrences {
private int id;
private String word;
private int howMany;
private char firstChar;
private List<String> filenames;
public Occurrences (int i, String w, int h, char f, List<String> filenames){
id = i;
word = w;
howMany = h;
firstChar = f;
this.filenames = filenames;
}
}
你考慮過嗎? 你有什么問題嗎?
有一些值得一提的事情。
首先,你要求一個ArrayList
,但文件名列表有幾個不同的特點:它只能包含每個文件名一次,文件名的順序無關緊要(我假設)。 這意味着它實際上是一個Set
,而不是List
; 這里使用的實現類是HashSet
。
其次,你仍然需要生成這個文件名集合,也許這就是你難以接受的地方。 您是從文件中讀取此信息嗎? 如果是這樣,假設您在字符串中有一個以逗號分隔的文件名列表。 喜歡:
String filenamesStr = "one.txt,two.txt,three.txt";
將它們轉換為集合的最簡單方法是將它們拆分為數組,將數組包裝在列表中,然后使用列表構造一個集合(是的,真的!):
Set<String> filenames = new HashSet<String>(Arrays.asList(filenamesStr.split(",")));
或者,您可能在處理文件時構建文件名。 在這種情況下,也許你應該做的是讓Occurrences類累積它們:
public class Occurrences {
private int id;
private String word;
private int howMany;
private char firstChar;
private Set<String> filenames;
public Occurrences (int i, String w){
id = i;
word = w;
firstChar = w.charAt(0); // bonus feature!
howMany = 0;
filenames = new HashSet<String>();
}
public void addOccurrence(String filename) {
++howMany;
filenames.add(filename);
}
}
然后,在索引文件時,只需addOccurrence
每次看到單詞時調用addOccurrence
。
第三,正如其他回答者指出的那樣, Map
將是組織整個詞匯世界的好方法。 您可以將單詞用作鍵,或者使用Occurences
或原始文件名集合作為值,具體取決於您真正需要的內容。
您真的不需要自己的數據類型。 這就是Map類的用途:
public void someMethod() {
Map<String, Collection<String>> filesByUniqueWord = new HashMap<String, Collection<String>>();
// inserting new entries
String uniqueWord = "hi";
List<String> filesContainingWord; // assume you have this
filesByUniqueWord.put(uniqueWord, filesContainingWord);
// deleting entries
filesByUniqueWord.remove(uniqueWord);
// getting all the files that contain some word
List<String> retrieved = filesByUniqueWord.get("hi"); // retrieved == filesContainingWord
}
首先不要調用類Collection,因為Collection作為java.util庫的一部分存在,所以會非常混亂。
其次,我假設你真的想寫一個集合,因為你正在學習如何去做,否則請使用HashMap或其他一些地圖,如其他一些答案所述。
所以假設你真的想自己編寫Collection,我建議實現一個標准的java接口。 java.util.Collection或Iterable 。 第二個是最簡單的。
所以像
class MyCollection extends Iterable { private String yourString; private String theArray; public MyCollection(String yourString, String[] theArray) { this.yourString = yourString; this.theArray = theArray } public boolean hasNext() { // See if there is a next element to return ... } public String next() { // return the next one } }
你需要的是String的StringList。 現在看到你的代碼,我假設我們將使用getter和setter。 因此,當您使用setter時,我認為不是在構造函數中為您的varibales賦值的最佳實踐。 而不是你可以在構造函數中實例化你的對象
public class Collection {
private int id;
private String word;
private int howMany;
private char firstChar;
private ArrayList<String> txtFiles;
public Collection (int i, String w, int h, char f){
word=new String();
txtFiles=new ArrayList<String>();
}
}
現在,對於您的ArrayList,您可以使用諸如的方法
Add(String file)
Remove(String file)
添加和刪除文件名
而設置其他變量則使用getter和setter之類的
setId(int id)
getId()
其他編碼邏輯由你決定,但我認為你應該如何設計你的課程
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.