java 如何将一个列表分配给另一个列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11150953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:58:50  来源:igfitidea点击:

how to assign a list to another list?

javalistcollectionsarraylist

提问by Mr.Cool

i have one problem with handling list,i have three class named as UserInf,userData,userProcess,i created one generic list using UserInf class, and i need to set that list to another list which is in class userprocess,this my sample code*

我有一个处理列表的问题,我有三个名为 UserInf、userData、userProcess 的类,我使用 UserInf 类创建了一个通用列表,我需要将该列表设置为 userprocess 类中的另一个列表,这是我的示例代码*

public class UserInf 
{
    private List<Data> pData;

    private String userId;

public void setData(List<Data> pData)
    {
        this.pData=pData;// Need to assign the fpData
    }
    public List<Data> getData()
    {
        return this.pData;
    }

    public void setUserId(String userId)
    {
        this.userId = userId;
    }
    public String getUserId()
    {
        return userId;
    }

in my other class userdata i cretaed list using userInf,add adding the value like this

在我的其他类用户数据中,我使用 userInf 创建了列表,添加这样的值

List<UserInf> userInformation=new ArrayList<UserInf>();
UserInf userInfo=new UserInf();
userInfo.setUserId(userid);
  userInfo.setData(pdata);
userInformation.add(userInfo);//up to this working fine


}

in my other class userprocees, i want to assign the userInformation list to another list i created object for userInf class ang get the property and assign to my new list

在我的其他类 userprocees 中,我想将 userInformation 列表分配给我为 userInf 类创建的对象的另一个列表,并获取属性并分配给我的新列表

List<UserInf> userProcessList=new ArrayList<UserInf>();
UserInf userProcess=new UserInf();
userProcess.getData();
userProcess.getUserId();
userProcessList.add(userProcess);//problem is here

but this is not working how to assign list to another list like this in java

但这不起作用如何在java中将列表分配给另一个这样的列表

回答by Aitsaam

You could try this:

你可以试试这个:

List<Int32> copy = new List<Int32>(original); 

List<Int32> copy = original.ToList(); 

回答by amicngh

List<UserInf> userInformation=new ArrayList<UserInf>();
UserInf userInfo=new UserInf();
userInfo.setUserId(userid);
userInfo.setData(pdata);
userInformation.add(userInfo);//up to this working fine

Now create new List by passing previous list as parameter.

现在通过将以前的列表作为参数传递来创建新列表。

List<String> userProcessList=new ArrayList<String>(userInformation);