在Java中动态创建新实例

时间:2015-06-04 20:21:18

标签: java constructor

我有一个名为CD的类,其中包含以下私有变量:

private String artist = "";
private String year = "";
private String albumName = "";
private ArrayList<String> songs = new ArrayList<String>();

此类用于存储采用以下格式的输入数据:

Led Zeppelin
1979 In Through the Outdoor
-In the Evening
-South Bound Saurez
-Fool in the Rain
-Hot Dog
-Carouselambra
-All My Love
-I'm Gonna Crawl

我有一个CDParser类,负责逐行解析名为sample.db的文件,以将其存储到我们的CD对象中。解析后,CD对象在用CD newCD = new CD()初始化后具有以下结构:

artist = "Led Zeppelin"

year = "1979"

albumName = "In Through the Outdoor"

songs = {"-In the Evening", "-South Bound Saurez", "-Fool in the Rain", "-Hot Dog"}

现在..对于这个项目,sample.db包含许多专辑,如下所示:

Led Zeppelin
1979 In Through the Outdoor
-In the Evening
-South Bound Saurez
-Fool in the Rain
-Hot Dog
-Carouselambra
-All My Love
-I'm Gonna Crawl

Led Zeppelin
1969 II
-Whole Lotta Love
-What Is and What Should Never Be
-The Lemon Song
-Thank You
-Heartbreaker
-Living Loving Maid (She's Just a Woman)
-Ramble On
-Moby Dick
-Bring It on Home

Bob Dylan
1966 Blonde on Blonde
-Rainy Day Women #12 & 35
-Pledging My Time
-Visions of Johanna
-One of Us Must Know (Sooner or Later)
-I Want You
-Stuck Inside of Mobile with the Memphis Blues Again
-Leopard-Skin Pill-Box Hat
-Just Like a Woman
-Most Likely You Go Your Way (And I'll Go Mine)
-Temporary Like Achilles
-Absolutely Sweet Marie
-4th Time Around
-Obviously 5 Believers
-Sad Eyed Lady of the Lowlands

到目前为止,我已经能够解析所有三张不同的相册,并将它们保存到我的CD对象中,但遇到了障碍,我只是将所有三张专辑保存到同一个{{1}对象。

我的问题是 - 有没有办法以编程方式初始化我的newCD构造函数,该构造函数将遵循格式CDnewCD1newCD2等,因为我解析了newCD3

这意味着,当我解析这个特定文件时:

  1. sample.db将是专辑newCD1(及其各自的私人代码)

  2. In Through the Outdoor将是专辑newCD2(及其各自的私人代码)

  3. II将是专辑newCD3,依此类推

  4. 这是一种聪明的方法吗?或者你能建议我一个更好的方法吗?

    编辑:

    附件是我的解析器代码。 Blonde on BlondeourDB,其中包含ArrayList的每一行:

    sample.db

3 个答案:

答案 0 :(得分:7)

你有一个CD对象,所以你要继续覆盖它。相反,你可以拥有CD的集合。 E.g:

List<CD> cds = new ArrayList<>();

CD newCD = new CD();
int line = 0;

for(String string : this.ourDB) {
    if(line == ARTIST) {
        newCD.setArtist(string);
        System.out.println(string);
        line++;
    } else if(line == YEAR_AND_ALBUM_NAME){
        String[] elements = string.split(" ");

        String[] albumNameArr = Arrays.copyOfRange(elements, 1, elements.length);

        String year = elements[0];
        String albumName = join(albumNameArr, " ");

        newCD.setYear(year);
        newCD.setAlbumName(albumName);

        System.out.println(year);
        System.out.println(albumName);

        line++;
    } else if(line >= SONGS && !string.equals("")) {
        newCD.setSong(string);
        System.out.println(string);
        line++;
    } else if(string.isEmpty()){
        // We're starting a new CD!
        // Add the one we have so far to the list, and start afresh
        cds.add(newCD);
        newCD = new CD();
        line = 0;
    }
}

// Take care of the case the file doesn't end with a newline:
if (line != 0) {
    cds.add(newCD);
}

答案 1 :(得分:5)

问题是您使用CD的相同对象引用来填充文件解析的值。

每次开始解析新相册的内容时,请确保初始化并存储CD newCD的每个实例。

您可以执行以下操作:

List<CD> cdList = new ArrayList<>();
for (<some way to handle you're reading a new album entry from your file>) {
    CD cd = new CD();
    //method below parses the data in the db per album entry
    //an album entry may contain several lines
    parseData(cd, this.ourDB);
    cdList.add(cd);
}
System.out.println(cdList);

您当前解析文件的方法有效但不具备应有的可读性。我建议使用两个循环:

List<CD> cdList = new ArrayList<>();
Iterator<String> yourDBIterator = this.ourDB.iterator();
//it will force to enter the first time
while (yourDBIterator.hasNext()) {
    //do the parsing here...
    CD cd = new CD();
    //method below parses the data in the db per album entry
    //an album entry may contain several lines
    parseData(cd, yourDBIterator);
    cdList.add(cd);
}

//...
public void parseData(CD cd, Iterator<String> it) {
    String string = it.next();
    int line = ARTIST;
    while (!"".equals(string)) {
        if (line == ARTIST) {
            newCD.setArtist(string);
            System.out.println(string);
            line++;
        } else if(line == YEAR_AND_ALBUM_NAME){
            String[] elements = string.split(" ");
            String[] albumNameArr = Arrays.copyOfRange(elements, 1, elements.length);
            String year = elements[0];
            String albumName = join(albumNameArr, " ");
            newCD.setYear(year);
            newCD.setAlbumName(albumName);
            System.out.println(year);
            System.out.println(albumName);
            line++;
        } else if(line >= SONGS && !string.equals("")) {
            newCD.setSong(string);
            System.out.println(string);
            line++;
        }
        if (it.hasNext()) {
            string = it.next();
        } else {
            string = "";
        }
    }
}

然后,你的代码

答案 2 :(得分:0)

我建议使用Builder设计模式来构建CD对象。如果您始终以相同的顺序读取行,则实现和使用并不复杂。好教程:http://www.javacodegeeks.com/2013/01/the-builder-pattern-in-practice.html