如何将ArrayList中的字符串值存储到一个ArrayList的对象中。 huangapple 117266文章 0评论 2020年9月3日 16:31:10go评论101阅读模式 英文: How to store String values from an ArrayList<String> to an ArrayList<Object> of Objects 问题 我试图获取 listString 的值并将它们保存到对象列表 listFeed 中,然后返回 listFeed。到目前为止,我使用了 Scanner 从 feedsFile 中添加数据到我的 ArrayList listString,但我不知道如何将这些值存储在一个对象的 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++) { // 这里是你需要处理的部分 // 将 listString 中的值转换为 Feed 对象并添加到 listFeed 中 } return listFeed;} 以下是 Feed 类: public class Feed implements Serializable, Comparable<Feed> { // 类的定义... // 其他方法和成员...} 在上述代码的第二个 for 循环中,你需要将 listString 中的每个值转换为 Feed 对象,并添加到 listFeed 中。你可以使用 Feed 类的构造函数来创建新的 Feed 对象,并设置相应的属性值。 以上是你需要的翻译内容。 英文: I'm trying to get the values of listString and save them to the list of objects listFeed and return listFeed. So far I used a Scanner to add date from feedsFile to my ArrayList listString but I don't know how to store those values in an ArrayList of Objects. Here's the code snippet 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;} Here's the Feed class: 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()); }} 答案1 得分: 1 最简单的方法是更改您的循环。for (int i = 0; i < listString.size(); i++) { listFeed.add(new Feed(listString.get(i)));}这样,您就会向listFeed中添加一个新的Feed对象。另一种方法是使用流API。List<Feed> feeds = listString.stream().map(Feed::new).collect(Collectors.toList());不过,您原来的循环技巧也是可以的。一个小提示,您不需要显式调用`super()`,如果您没有使用不同版本的super,它会被自动调用。 英文: The simplest way is to change your loop. for(int i = 0; i < listString.size(); i++) { listFeed.add( new Feed( listString.get(i) );} That way you're adding a new Feed object to the listFeed. Another way you could do it is to use the stream api. List<Feed> feeds = listString.stream().map( Feed::new ).collect( Collectors.toList() ); Your original technique with a loop is fine though. A minor note, you do not need to call super() explicitly it will be called automatically if you don't use a different version of super. 通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。 点赞 https://go.coder-hub.com/63719772.html 复制链接 复制链接 本文由 huangapple 发表于 2020年9月3日 16:31:10 转载请务必保留本文链接:https://go.coder-hub.com/63719772.html arraylist java .equals与其他布尔函数 go 103 10/14 无效的 JavaFx 应用中的 SQL 命令 go 97 09/12 如何在Java中通过Scanner输入null? go 104 07/27 在Java中替换或重命名两个标记之间的字符串并作为字符串返回。 go 138 07/22 如何在Playwright视觉比较中屏蔽多个定位器? 在C++中,可以使用可变模板参数来检索类型的内部类型。 selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: stale element not found Creating and opening a URL to log in to Website via Basic Auth with Robot Framework/Selenium (Python) AG Grid 在上下文菜单中以大文本形式打开 What's the correct way to type hint an empty list as a literal in python? 如何在Highcharts Gantt中更改本地化的星期名称 如何在同一个流中使用多个过滤器和映射函数? 如何使用Map/Set来将代码优化到O(n)? .NET MAUI Android在GitHub Actions上构建失败,错误代码为1。 如何在Playwright视觉比较中屏蔽多个定位器? 在C++中,可以使用可变模板参数来检索类型的内部类型。 selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: stale element not found Creating and opening a URL to log in to Website via Basic Auth with Robot Framework/Selenium (Python) AG Grid 在上下文菜单中以大文本形式打开 What's the correct way to type hint an empty list as a literal in python? 如何在Highcharts Gantt中更改本地化的星期名称 如何在同一个流中使用多个过滤器和映射函数? 如何使用Map/Set来将代码优化到O(n)? .NET MAUI Android在GitHub Actions上构建失败,错误代码为1。 发表评论 匿名网友 # 确定 昵称 邮箱 网址 Address 取消