我有一个名为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
构造函数,该构造函数将遵循格式CD
,newCD1
,newCD2
等,因为我解析了newCD3
?
这意味着,当我解析这个特定文件时:
sample.db
将是专辑newCD1
(及其各自的私人代码)
In Through the Outdoor
将是专辑newCD2
(及其各自的私人代码)
II
将是专辑newCD3
,依此类推
这是一种聪明的方法吗?或者你能建议我一个更好的方法吗?
编辑:
附件是我的解析器代码。 Blonde on Blonde
是ourDB
,其中包含ArrayList
的每一行:
sample.db
答案 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