2011-11-14 58 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编码逻辑取决于你,但我认为你应该如何设计你的类