2011-11-14 53 views
2

我不熟悉編程和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); 
+0

如果你正試圖實現一個搜索/索引,你可以利用lucene。您可以通過擴展集合或封裝它來創建自己的集合。 – aishwarya

+0

什麼是您在最後一個片段中調用的'Collection'構造函數?你還沒有顯示一個構造函數,它之前需要兩個字符串。 –

回答

1

你需要做什麼可以通過使用HashMap來完成。其中的一個例子可以獲得here

你可以做一些事情,像這樣:

Map<String, List<String>> myMap = new HashMap<String, List<String>(); 

然後,加載的文件和你所需要的。

1

你並不需要你自己的數據類型。這是地圖類是什麼:

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 
} 
4

我去之前,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(","))); 

或者,您可能會在處理文件時建立文件名。在這種情況下,也許你應該做的是讓紀錄類積累他們:

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每次你看到一個詞的時間。

第三,正如其他答覆者指出的那樣,Map將是組織整個詞彙的好方法。您可以使用該詞作爲鍵,並且可以使用Occurences或文件名的原始集合作爲值,具體取決於您真正需要的。

+0

這正是我的想法,但我不認爲這會起作用的原因是創建類的新實例時,我怎樣才能將一個字符串添加到數組列表中。簡單地做到這一點是行不通的。 Collection newTest = new Collection(「test」,「test」); \t \t \t集合。添加(newTest); – DomX23

+0

這是一個古老的技巧,當你想添加一些值到一個集合關閉蝙蝠...集合 newTest = new ArrayList (){{add(「test」); add(「test」)}}; – jkschneider

+0

@jkschneider:啊,舊的雙Brace Initializer。鑑於存在「Arrays.asList」,這似乎是一種相當無用的使用方式;然而,它初始化地圖肯定非常有用。 –

1

首先,不要調用類Collection,這會非常令人困惑,因爲Collection作爲java.util庫的一部分存在。第二,我假設你實際上想寫一個集合,因爲你正在學習如何去做,否則請使用一些HashMap或其他類型的地圖,如其他一些答案中所述。所以假設你真的想自己寫集合,我建議實現一個標準的java接口。 java.util.CollectionIterable。第二個將是最簡單的。

因此,像

 
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 
    } 
} 

1

你需要的是字符串的ArrayList。現在看到你的代碼,我假設我們將使用getters和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) 

添加和刪除文件名

和設置其他變量使用獲得者和​​設定者,如

setId(int id) 
getId() 

其他c編碼邏輯取決於你,但我認爲你應該如何設計你的類