开发者

Sending a group of objects in a method?

I'm trying to get a reference to a group of ArrayLists into another object to be used by that object?

At first I collected the group in another ArrayList. But when extrapolating them their is no way to define which ArrayList is which, as all the ArrayLists contain the same object. The ArrayLists don't have names.

Background to the Problem:

Imagine i have a mass of data..I arrange the data and assign values to objects. This data upon completion needs to be seperated into 4 categorys, which can be down at the first processing stage. So imagine 4 arraylists of objects. The next stage involves more work by a seperate object on these data objects. So my problem is transferring them in a way that is future proof so if more categorys become a neccesity its not so hard to do. So in the first processing object I add them to a list. List is passed into the second processing object a开发者_JS百科t a later stage.


I am not sure if this is the optimal approach but you can use a Map so that you can name the ArrayLists


If naming is a solution, consider putting the lists into a map:

Map<String, List<?>> lists

The key is a name, chosen by you, to identify a list.


Your question is very vague, I don't really know what you are trying to do. But one thing is obvious: you are trying to model something using collections where you should be using custom objects. Here's an example:

public class Member{

    private final String name;

    public Member(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }

}

public class Group{
    private final String name;
    private final List<Member> members;
    public Group(String name, List<Member> members){
        this.name = name;
        this.members = members;
    }

    public String getName(){
        return name;
    }

    public List<Member> getMembers(){
        return new ArrayList<Member>( members);
    }
}

Now if you pass a List of Group objects to a method, you can use the getName() method to see who you are dealing with. And so on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜