如何将ArrayList中的字符串值存储到一个ArrayList的对象中。
huangapple go评论101阅读模式
英文:

How to store String values from an ArrayList<String> to an ArrayList<Object> of Objects

问题

我试图获取 listString 的值并将它们保存到对象列表 listFeed 中,然后返回 listFeed。到目前为止,我使用了 Scanner 从 feedsFile 中添加数据到我的 ArrayList listString,但我不知道如何将这些值存储在一个对象的 ArrayList 中。

以下是代码片段:

  1. public List<Feed> loadSubscribedFeeds(File feedsFile) throws FileNotFoundException {
  2. Scanner s = new Scanner(feedsFile);
  3. List<String> listString = new ArrayList<>();
  4. List<Feed> listFeed = new ArrayList<>();
  5. while (s.hasNextLine()) {
  6. listString.add(s.nextLine());
  7. }
  8. for (int i = 0; i < listString.size(); i++) {
  9. // 这里是你需要处理的部分
  10. // 将 listString 中的值转换为 Feed 对象并添加到 listFeed 中
  11. }
  12. return listFeed;
  13. }

以下是 Feed 类:

  1. public class Feed implements Serializable, Comparable<Feed> {
  2. // 类的定义...
  3. // 其他方法和成员...
  4. }

在上述代码的第二个 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

  1. public List&lt;Feed&gt; loadSubscribedFeeds(File feedsFile) throws FileNotFoundException {
  2. Scanner s = new Scanner(feedsFile);
  3. List&lt;String&gt; listString = new ArrayList&lt;&gt;();
  4. List&lt;Feed&gt; listFeed = new ArrayList&lt;&gt;();
  5. while (s.hasNextLine()) {
  6. listString.add(s.nextLine());
  7. }
  8. for(int i = 0; i &lt; listString.size(); i++) {
  9. for(int j = 0; j &lt; listFeed.size(); j++) {
  10. }
  11. }
  12. return listFeed;
  13. }

Here's the Feed class:

  1. public class Feed implements Serializable, Comparable&lt;Feed&gt; {
  2. private static final long serialVersionUID = 1L;
  3. private String url;
  4. private String title;
  5. private String description;
  6. private String publishedDateString;
  7. private List&lt;Entry&gt; entries;
  8. public Feed(String url) {
  9. super();
  10. this.url = url;
  11. this.entries = new ArrayList&lt;Entry&gt;();
  12. this.title = &quot;&quot;;
  13. this.description = &quot;&quot;;
  14. this.publishedDateString = &quot;&quot;;
  15. }
  16. /**
  17. * Creates an instance of a Feed and transfers the feed
  18. * data form a SyndFeed object to the new instance.
  19. *
  20. * @param url The URL string of this feed
  21. * @param sourceFeed The SyndFeed object holding the data for this feed instance
  22. */
  23. public Feed(String url, SyndFeed sourceFeed) {
  24. this(url);
  25. setTitle(sourceFeed.getTitle());
  26. setDescription(sourceFeed.getDescription());
  27. if (sourceFeed.getPublishedDate() != null)
  28. setPublishedDateString(FeaderUtils.DATE_FORMAT.format(sourceFeed.getPublishedDate()));
  29. for (SyndEntry entryTemp : sourceFeed.getEntries()) {
  30. Entry entry = new Entry(entryTemp.getTitle());
  31. entry.setContent(entryTemp.getDescription().getValue());
  32. entry.setLinkUrl(entryTemp.getLink());
  33. entry.setParentFeedTitle(getTitle());
  34. if (entryTemp.getPublishedDate() != null) {
  35. entry.setPublishedDateString(FeaderUtils.DATE_FORMAT.format(entryTemp.getPublishedDate()));
  36. }
  37. addEntry(entry);
  38. }
  39. }
  40. public String getUrl() {
  41. return url;
  42. }
  43. public void setTitle(String title) {
  44. this.title = title != null ? title : &quot;&quot;;
  45. }
  46. public String getTitle() {
  47. return title;
  48. }
  49. public void setDescription(String description) {
  50. this.description = description != null ? description : &quot;&quot;;
  51. }
  52. public String getDescription() {
  53. return description;
  54. }
  55. public void setPublishedDateString(String publishedDateString) {
  56. this.publishedDateString = publishedDateString != null ? publishedDateString : &quot;&quot;;
  57. }
  58. public String getPublishedDateString() {
  59. return publishedDateString;
  60. }
  61. /**
  62. * Returns a short string containing a combination of meta data for this feed
  63. *
  64. * @return info string
  65. */
  66. public String getShortFeedInfo() {
  67. return getTitle() + &quot; [&quot; +
  68. getEntriesCount() + &quot; entries]: &quot; +
  69. getDescription() +
  70. (getPublishedDateString() != null &amp;&amp; getPublishedDateString().length() &gt; 0
  71. ? &quot; (updated &quot; + getPublishedDateString() + &quot;)&quot;
  72. : &quot;&quot;);
  73. }
  74. public void addEntry(Entry entry) {
  75. if (entry != null) entries.add(entry);
  76. }
  77. public List&lt;Entry&gt; getEntries() {
  78. return entries;
  79. }
  80. public int getEntriesCount() {
  81. return entries.size();
  82. }
  83. @Override
  84. public boolean equals(Object obj) {
  85. return (obj instanceof Feed)
  86. &amp;&amp; ((Feed) obj).getUrl().equals(url);
  87. }
  88. @Override
  89. public int hashCode() {
  90. return url.hashCode();
  91. }
  92. @Override
  93. public String toString() {
  94. return getTitle();
  95. }
  96. @Override
  97. public int compareTo(Feed o) {
  98. return getPublishedDateString().compareTo(o.getPublishedDateString());
  99. }
  100. }

答案1

得分: 1

  1. 最简单的方法是更改您的循环。
  2. for (int i = 0; i < listString.size(); i++) {
  3. listFeed.add(new Feed(listString.get(i)));
  4. }
  5. 这样,您就会向listFeed中添加一个新的Feed对象。
  6. 另一种方法是使用流API
  7. List<Feed> feeds = listString.stream().map(Feed::new).collect(Collectors.toList());
  8. 不过,您原来的循环技巧也是可以的。
  9. 一个小提示,您不需要显式调用`super()`,如果您没有使用不同版本的super,它会被自动调用。
英文:

The simplest way is to change your loop.

  1. for(int i = 0; i &lt; listString.size(); i++) {
  2. listFeed.add( new Feed( listString.get(i) );
  3. }

That way you're adding a new Feed object to the listFeed.

Another way you could do it is to use the stream api.

  1. List&lt;Feed&gt; 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.

huangapple
  • 本文由 发表于 2020年9月3日 16:31:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63719772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

  • 开发者交流平台

    本页二维码