简体   繁体   中英

How to add an empty arrayList to another arrayList created from a class

I am trying to add an empty arrayList "patternList" to the "treeList" arrayList created from the treeNode class but I am having trouble populating the treeList arrayList with an empty patternList arrayList shown in the last line of the code. Basically I want an empty arrayList for each item in the treeList that I will be populating later on. Thanks.

import java.util.*;
import java.util.ArrayList;
import java.util.List;

public class testJava{

    public static class treeNode {
        String nodeName;
        String pNodeName;
        String fNodeName;
        boolean begNode;
        boolean targetNode;
        int nodeNumber;
        //String pattern;
        ArrayList<String> patternList = new ArrayList<String>();
        public treeNode (String nodeName, String pNodeName, boolean begNode, boolean targetNode, int nodeNumber, ArrayList<String> patternList)
        {
            this.nodeName = nodeName;
            this.pNodeName = pNodeName;
            this.begNode = begNode;
            this.targetNode = targetNode;
            this.nodeNumber = nodeNumber;
            this.patternList = patternList;
            //this.pattern = pattern;
        }
    }

    public static void main (String[] args){
        ArrayList<treeNode> treeList = new ArrayList<treeNode>();
        ArrayList<String> openList = new ArrayList<String>();
        ArrayList<String> closeList = new ArrayList<String>();
        ArrayList<String> treePath = new ArrayList<String>();
        String currentNode = "";
        String targetNode = "";
        int smallestNodeCount  = 0;
        int tempNodeCount = 0;
        int openListCount = 0;
        String currentPattern = "" ;

        treeList.add(new treeNode ("S 1", null, true, false, 1, ArrayList<string> patternList));

To create a new empty ArrayList , the syntax would be new ArrayList<String>() .

Since you're immediately passing the newly created list as a parameter to your TreeNode constructor, there's no need for a variable name.

Your last line should therefore be:

treeList.add(new treeNode ("S 1", null, true, false, 1, new ArrayList<String>()));

Also, by convention, Java class names are in UpperCamelCase, not in lowerCamelCase. That's why it's ArrayList rather than arrayList .

Your class names should be TreeNode and TestJava , with an upper-case T .

Another thing to pay attention to is your variable types; more often than not, you want your variable types to use the interface when available, rather than being explicitly tied to a particular implementation class. So instead of defining your list using ArrayList<String> openList = new ArrayList<String>(); , consider making the type of openList simply List . This only applies on the left-hand side of the declaration. On the right-hand side, you still need the concrete class (ie ArrayList ) when creating the new instance. Your declaration therefore becomes:

List<String> openList = new ArrayList<>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM