制作我自己的数据类型

时间:2011-11-14 08:06:48

标签: java

我是编程和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;
    }

}

我仍然不确定如何使用它,因为在创建my Collection类的新实例时,我不能简单地向ArrayList参数添加字符串。比如这个:

ArrayList<Collection> collection = new ArrayList<Collection>();
Collection newTest= new Collection("test","test");
collection.add(newTest);

5 个答案:

答案 0 :(得分: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(",")));

或者,您可能在处理文件时构建文件名。在这种情况下,也许您应该做的是让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

第三,正如其他回答者所指出的那样,Map将是组织整个词汇世界的好方法。您可以将单词用作键,并使用Occurences或原始文件名集合作为值,具体取决于您真正需要的内容。

答案 1 :(得分:1)

您需要做的是使用HashMap。可以获得一个例子here

你可以这样做:

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

然后,加载文件并执行您需要的操作。

答案 2 :(得分:1)

您真的不需要自己的数据类型。这就是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
}

答案 3 :(得分:1)

首先不要调用类Collection,因为Collection作为java.util库的一部分存在,所以会非常混乱。

其次,我假设你真的想写一个集合,因为你正在学习如何去做,否则请使用HashMap或其他一些地图,如其他一些答案所述。

因此,假设您实际上想自己编写Collection,我建议实现一个标准的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
    }
}

答案 4 :(得分:1)

您需要的是String的ArrayList。现在看到你的代码,我假设我们将使用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()

其他编码逻辑由你决定,但我认为你应该如何设计你的课程

相关问题