Julio
Julio

Reputation: 1855

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 at a later stage.

Upvotes: 0

Views: 117

Answers (3)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298828

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.

Upvotes: 0

Andreas Dolk
Andreas Dolk

Reputation: 114757

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.

Upvotes: 1

Aravind Yarram
Aravind Yarram

Reputation: 80176

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

Upvotes: 3

Related Questions