Java 如何存储ArrayList中的字符串值<;字符串>;到ArrayList<;对象>;物体的形状
我试图获取Java 如何存储ArrayList中的字符串值<;字符串>;到ArrayList<;对象>;物体的形状,java,arraylist,Java,Arraylist,我试图获取listString的值,并将它们保存到对象列表listFeed,然后返回listFeed。到目前为止,我使用扫描仪将日期从feedsFile添加到我的ArrayListlistString中,但我不知道如何将这些值存储在对象的ArrayList中 下面是代码片段 public List<Feed> loadSubscribedFeeds(File feedsFile) throws FileNotFoundException { Scanner s =
listString
的值,并将它们保存到对象列表listFeed
,然后返回listFeed
。到目前为止,我使用扫描仪将日期从feedsFile
添加到我的ArrayListlistString
中,但我不知道如何将这些值存储在对象的ArrayList中
下面是代码片段
public List<Feed> loadSubscribedFeeds(File feedsFile) throws FileNotFoundException {
Scanner s = new Scanner(feedsFile);
List<String> listString = new ArrayList<>();
List<Feed> listFeed = new ArrayList<>();
while (s.hasNextLine()) {
listString.add(s.nextLine());
}
for(int i = 0; i < listString.size(); i++) {
for(int j = 0; j < listFeed.size(); j++) {
}
}
return listFeed;
}
public List loadSubscribedFeeds(文件feedsFile)抛出FileNotFoundException{
扫描仪s=新扫描仪(feedsFile);
List listString=new ArrayList();
List listFeed=new ArrayList();
而(s.hasNextLine()){
add(s.nextLine());
}
对于(int i=0;i
下面是提要类:
public class Feed implements Serializable, Comparable<Feed> {
private static final long serialVersionUID = 1L;
private String url;
private String title;
private String description;
private String publishedDateString;
private List<Entry> entries;
public Feed(String url) {
super();
this.url = url;
this.entries = new ArrayList<Entry>();
this.title = "";
this.description = "";
this.publishedDateString = "";
}
/**
* Creates an instance of a Feed and transfers the feed
* data form a SyndFeed object to the new instance.
*
* @param url The URL string of this feed
* @param sourceFeed The SyndFeed object holding the data for this feed instance
*/
public Feed(String url, SyndFeed sourceFeed) {
this(url);
setTitle(sourceFeed.getTitle());
setDescription(sourceFeed.getDescription());
if (sourceFeed.getPublishedDate() != null)
setPublishedDateString(FeaderUtils.DATE_FORMAT.format(sourceFeed.getPublishedDate()));
for (SyndEntry entryTemp : sourceFeed.getEntries()) {
Entry entry = new Entry(entryTemp.getTitle());
entry.setContent(entryTemp.getDescription().getValue());
entry.setLinkUrl(entryTemp.getLink());
entry.setParentFeedTitle(getTitle());
if (entryTemp.getPublishedDate() != null) {
entry.setPublishedDateString(FeaderUtils.DATE_FORMAT.format(entryTemp.getPublishedDate()));
}
addEntry(entry);
}
}
public String getUrl() {
return url;
}
public void setTitle(String title) {
this.title = title != null ? title : "";
}
public String getTitle() {
return title;
}
public void setDescription(String description) {
this.description = description != null ? description : "";
}
public String getDescription() {
return description;
}
public void setPublishedDateString(String publishedDateString) {
this.publishedDateString = publishedDateString != null ? publishedDateString : "";
}
public String getPublishedDateString() {
return publishedDateString;
}
/**
* Returns a short string containing a combination of meta data for this feed
*
* @return info string
*/
public String getShortFeedInfo() {
return getTitle() + " [" +
getEntriesCount() + " entries]: " +
getDescription() +
(getPublishedDateString() != null && getPublishedDateString().length() > 0
? " (updated " + getPublishedDateString() + ")"
: "");
}
public void addEntry(Entry entry) {
if (entry != null) entries.add(entry);
}
public List<Entry> getEntries() {
return entries;
}
public int getEntriesCount() {
return entries.size();
}
@Override
public boolean equals(Object obj) {
return (obj instanceof Feed)
&& ((Feed) obj).getUrl().equals(url);
}
@Override
public int hashCode() {
return url.hashCode();
}
@Override
public String toString() {
return getTitle();
}
@Override
public int compareTo(Feed o) {
return getPublishedDateString().compareTo(o.getPublishedDateString());
}
}
public类提要实现了可序列化、可比较的{
私有静态最终长serialVersionUID=1L;
私有字符串url;
私有字符串标题;
私有字符串描述;
私有字符串测试环;
私人名单条目;
公共提要(字符串url){
超级();
this.url=url;
this.entries=new ArrayList();
this.title=“”;
此名称为“”;
this.testring=“”;
}
/**
*创建提要实例并传输提要
*数据从SyndFeed对象发送到新实例。
*
*@param url此提要的url字符串
*@param sourceFeed保存此提要实例数据的SyndFeed对象
*/
公共提要(字符串url、SyndFeed源提要){
此(url);
setTitle(sourceFeed.getTitle());
setDescription(sourceFeed.getDescription());
if(sourceFeed.getPublishedDate()!=null)
setPublishedDataString(FeaderUtils.DATE_FORMAT.FORMAT(sourceFeed.getPublishedDate());
对于(SyndEntry entryTemp:sourceFeed.getEntries()){
Entry=新条目(entryTemp.getTitle());
entry.setContent(entryTemp.getDescription().getValue());
entry.setLinkUrl(entryTemp.getLink());
setParentFeedTitle(getTitle());
if(entryTemp.getPublishedDate()!=null){
entry.setPublishedDataString(FeaderUtils.DATE_FORMAT.FORMAT(entryTemp.getPublishedDate());
}
附录(条目);
}
}
公共字符串getUrl(){
返回url;
}
公共无效集合标题(字符串标题){
this.title=title!=null?标题:“”;
}
公共字符串getTitle(){
返回标题;
}
公共void集合描述(字符串描述){
this.description=说明!=null?说明:“”;
}
公共字符串getDescription(){
返回说明;
}
公共void setPublishedDataString(字符串PublishedDataString){
this.publishedDateString=publishedDateString!=null?publishedDateString:;
}
公共字符串getPublishedDataTestString(){
返回测试环;
}
/**
*返回包含此提要元数据组合的短字符串
*
*@返回信息字符串
*/
公共字符串getShortFeedInfo(){
返回getTitle()+“[”+
getEntriesCount()+“条目]:”+
getDescription()+
(GetPublishedDataTestRing()!=null&&GetPublishedDataTestRing().length()>0
?“(更新的“+GetPublishedDataTestRing()+”)
: "");
}
公共作废补遗(条目){
如果(条目!=null)条目。添加(条目);
}
公共列表getEntries(){
返回条目;
}
public int getEntriesCount(){
返回条目。size();
}
@凌驾
公共布尔等于(对象obj){
返回(馈送的obj实例)
&&((Feed)obj.getUrl().equals(url);
}
@凌驾
公共int hashCode(){
返回url.hashCode();
}
@凌驾
公共字符串toString(){
返回getTitle();
}
@凌驾
公共int比较(馈送o){
返回getPublishedDataTestRing().compareTo(o.getPublishedDataTestRing());
}
}
最简单的方法是更改循环
for(int i = 0; i < listString.size(); i++) {
listFeed.add( new Feed( listString.get(i) );
}
for(int i=0;i
这样,您将向listFeed添加一个新的提要对象
另一种方法是使用流api
List<Feed> feeds = listString.stream().map( Feed::new ).collect( Collectors.toList() );
List feeds=listString.stream().map(Feed::new).collect(Collectors.toList());
不过,使用循环的原始技术很好
请注意,您不需要显式调用
super()
,如果您不使用不同版本的super,它将自动调用。Where(在哪个字段中)确切地说,在要存储数组列表
项的提要
类中,您不需要在列表提要
项上循环,您可以调用列表字符串
列表中的每个项。另一种方法是调用,然后使用lambda,该lambda接受字符串
,并返回提要
,然后使用.collect(Collectors.toList())
谢谢。添加(新提要(…)可以正常工作。