0

I have to make a family tree. I'm going to do this with an ArrayList of ArrayLists. Each ArrayList in the main ArrayList should be named after the person it represents. These "sub ArrayLists" will contain the locations of the parents and children of the person whom it is named after

Ideally what I'm trying to do looks something like this.

public void makeArrayList(String arrayName){
   ArrayList arrayName = new ArrayList();
}

But I cannot do this as "arrayName" is already declared in the method.

Is this what you were talking about doing?

public class Member{
public Member(String name, ArrayList<String> infoList)
public ArrayList<Member> MemberList = new ArrayList<Member>();

public void getName(){
    return Member.name;
}

public void getList(){
    return Member.infoList;
}
Heiland
  • 19
  • 4
  • You can't do this for [so many reasons](http://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java). And even if you could, for the reason you pointed out this would not be possible because the name `arrayName` is already taken up by the input variable. – Tim Biegeleisen Mar 24 '16 at 01:38
  • I just need a way to create an ArrayList with a name that the user chooses. – Heiland Mar 24 '16 at 01:41
  • Extending from what @TimBiegeleisen mentioned. You cannot have dynamic variable names in Java. Moreover, why do you want dynamic variable names? If it's happening at runtime, you "cant see it" anyway. So, what's the point? – Debosmit Ray Mar 24 '16 at 01:42
  • I have to make a family tree. I'm going to do this with an ArrayList of ArrayLists. Each ArrayList in the main ArrayList should be named after the person it represents. These "sub ArrayLists" will contain the locations of the parents and children of the person whom it is named after. – Heiland Mar 24 '16 at 01:46
  • please edit add this to the post. – Debosmit Ray Mar 24 '16 at 01:50
  • @TimBiegeleisen I added an answer. It would be great if you could take a look at it, to see if I made a valid suggestion. – Debosmit Ray Mar 24 '16 at 01:53
  • I'll try it out and report back later. – Heiland Mar 24 '16 at 01:57
  • 1
    `ArrayList` objects don't have names. *Variables* have named, and can be updated to refer to any object compatible with the type of the variable. To do a Family Tree, you should use the Object-Oriented nature of Java and create a Class. You should start your reading here (or a similar place): [The Java™ Tutorials - Object-Oriented Programming Concepts](https://docs.oracle.com/javase/tutorial/java/concepts/index.html) – Andreas Mar 24 '16 at 02:08

1 Answers1

2

You mentioned that you have to build a family tree, and to do that you are using an ArrayList of ArrayLists (of strings, I presume). There is a design flaw if you need to give these sub-arraylists "variable names"? That is not a unique identifier.

One solution. When you initialize lists in the Arraylist of ArrayLists, say the first index can hold the "variable name".

Second. And far more preferable choice. Create an object. This object will have 2 fields, one a string, and the other the aforementioned arraylist of strings. The string is where you would want to put your 'variable name'. Your design will then have an arraylist of these objects.

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43