在Java中动态创建新实例
我有一个名为在Java中动态创建新实例,java,constructor,Java,Constructor,我有一个名为CD的类,包含以下私有变量: private String artist = ""; private String year = ""; private String albumName = ""; private ArrayList<String> songs = new ArrayList<String>(); 我有一个CDParser类,负责逐行解析名为sample.db的文件,将其存储到我们的CD对象中。解析后,CD对象在使用CD newCD=new
CD
的类,包含以下私有变量:
private String artist = "";
private String year = "";
private String albumName = "";
private ArrayList<String> songs = new ArrayList<String>();
我有一个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
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);
到目前为止,我已经能够解析所有三个不同的相册并将它们保存到我的CD
对象中,但遇到了一个障碍,我只是将所有三个相册保存到同一个newCD
对象中
我的问题是-在解析sample.db
时,是否有一种方法可以通过编程方式初始化我的CD
构造函数,该构造函数将遵循newCD1
、newCD2
、newCD3
等格式
这意味着,当我解析这个特定文件时:
newCD1
将是通过户外进入的专辑(及其各自的私人VAR)
newCD2
将是专辑II
(及其各自的私有变量)newCD3
将是专辑Blonde on Blonde
,依此类推ourDB
是一个ArrayList
,包含示例的每一行。db
:
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()){
line = 0;
}
}
问题是,您使用的是与
CD
相同的对象引用来填充文件解析的值
只要确保每次开始解析新相册的内容时都初始化并存储CD newCD
的每个实例
您可以执行以下操作:
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
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-cdList=new-ArrayList();
对于(我建议使用Builder设计模式来构造CD对象。如果您总是以相同的顺序读取行,那么实现和使用就不复杂了。很好的教程:您有一个CD
对象,所以您会不断覆盖它。相反,您可以保存一个CD
对象的集合。例如:
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);
}
List cds=new ArrayList();
CD newCD=新CD();
内线=0;
for(字符串:this.ourDB){
如果(线==艺术家){
newCD.setArtist(字符串);
System.out.println(字符串);
line++;
}else if(行==年份和相册名称){
String[]elements=String.split(“”);
字符串[]albumNameArr=Arrays.copyOfRange(elements,1,elements.length);
字符串年份=元素[0];
字符串albumName=join(albumNameArr,“”);
新cd.setYear(年);
newCD.setAlbumName(albumName);
系统输出打印项次(年);
System.out.println(相册名称);
line++;
}else if(行>=歌曲&&!string.equals(“”){
newCD.setSong(字符串);
System.out.println(字符串);
line++;
}else if(string.isEmpty()){
//我们要开始一张新的CD!
//将我们目前为止拥有的一个添加到列表中,然后重新开始
cds.add(newCD);
newCD=新CD();
直线=0;
}
}
//请注意文件不以换行符结尾的情况:
如果(行!=0){
cds.add(newCD);
}
我确实使用了CD
类的同一个实例。这是我的问题-如何动态生成一个新实例来存储每个相册?@TheGreenColla just doCD newCD=new CD()
正确。谢谢-这正是我的意思。我不清楚你想做什么。如果可能的话,添加当前代码段似乎你在对象之间没有正确循环。向我们展示你的更多代码。你在寻找数组和列表吗?我会引导你不要试图通过构造函数来做这件事。相反,只需使用你可以根据需要添加更多专辑和歌曲的POJO你已经在你正在使用的CD类中有了一个列表
。也可以在外部使用一个列表来存储你的CD。否则,使用映射
来索引CD!这就是我的意思!啊..我之所以困惑,是因为我认为每个实例都存储在我的集合中on需要有一个不同的名称,即,如果我的newCD
s列表中有一个newCD
,下一个实例不能包含相同的名称。嗨,Mureinik。因此,我遇到了一个问题,我只打印出前两张专辑-第三张专辑似乎丢失了。我继续在e中添加了打印语句在最后一个else if之前,使用每个if语句,我得到了正确的输出,但是如果我在string.isEmpty()
if语句中尝试我的CD
对象,我只得到两个CD对象。@出于某种原因,我假设文件末尾会有一个空行。请参阅我编辑的答案。完美。你是最好的!