Java Array.asList错误

时间:2018-03-27 05:19:26

标签: java spring-boot

我使用asList()创建了一个数组列表但是我遇到了很多错误

@Service
public class ArticleService {

List<Article> articleList = new ArrayList<Article>(Arrays.asList(
new Article( id:"1", name: "Article 01", description: "Description 01" ),
new Article( id:"2", name: "Article 02", description: "Description 02" ),
new Article( id:"3", name: "Article 03", description: "Description 03" )
));

public List<Article> getAllArticles(){
return articleList;
}
}

3 个答案:

答案 0 :(得分:1)

假设您的文章课程如下所示,请进行适当的课程,

class Article{
       String id;
       String article;
       String description;
   Article(String id ,String articleName,String description)
   {
      this.id=id;
      this.articleName = articleName;
      this.description = description;
   }
   ........
   //your getter/setters are defined here.

}

现在你应该在你的主类中使用它们,如下所示,

List<Article> articleList = new ArrayList<Article>(Arrays.asList(
                new Article( "1","Article 01", "Description 01" ),
                new Article( "2","Article 02","Description 02" ),
                new Article("3","Article 03","Description 03" )
                ));

 //this is the correct way of using it.

答案 1 :(得分:0)

使用Arrays.asList的正确方法:

List<Article> articleList = Arrays.asList(new Article(),new Article(),new Article());

此外,您没有正确地将参数传递给承包商,如果您有三个字符串,那么它应该是这样的:

new Article("arg0","arg1","arg1");

答案 2 :(得分:0)

// you can execute your business logic like below code

List<Article> articleList=null;
        articleList = (List<Article>) new ArrayList();

        Article art = new Article();
        art.setId(0);
        art.setName("Article 01");
        art.setDescription("Description 01");
        articleList.add(art);